0

It seems the python implementation of TensorFlow has bitwise_or implementation, however, the TensorFlow.js does not.

I need to compare two 1D tensors and perform a bitwise OR operation (element-wise). Does anyone now how to do this without looping in javascript (it needs to be done on arrays of lengths in the hundreds of millions, hence avoiding loops)? I've only found very complicated workarounds thus far...

Here's what it looks like in Python with numpy for reference:

array_or = array_1 | array_2
joshp
  • 706
  • 5
  • 22
  • Do you assume, that just because you don't have to spell out the looping, python doesn't have to iterate over the two lists? You don't get around it. – Thomas Jan 14 '21 at 23:31
  • Yes, that's clear, but Python is written on top of C and the libraries are optimized for large vector computations – joshp Jan 14 '21 at 23:37

2 Answers2

0

EDIT: for large arrays and performant code, I used WebAssembly

Well, I have something, going to test the performance but it seems like mathjs does have the functionality necessary

Turns the following python code

out = array1 | ((array2 << 8) & 0xF)

into the following javascript code

const out = math.bitOr(
  array1,
  math.bitAnd(math.leftShift(array2, 8), 15)
);
joshp
  • 706
  • 5
  • 22
  • I'd have to look into the source of that library, but such libraries usually try to be convenient, flexible, suitable for different use-cases and programming styles. And such flexibility always comes at the cost of performance. Performance wise, a simple loop may be your best option in JS. – Thomas Jan 15 '21 at 07:40
0

mathjs ... into the following javascript code...

In plain JS you won't get any code more performant than:

const length = Math.min(array_1.length, array_2.length);
const array_or = new Int32Array(length);

for (let i = 0; i < length; ++i) {
  array_or[i] = array_1[i] | array_2[i];
}

but Python is written on top of C

so are modern JS engines

and the libraries are optimized for large vector computations

that's not the case in JS. JS wasn't the platform to do such math-heavy operations.

You can't even overload operators like you'd need for something like object operator object to work. In JS array_1 | array_2 would be performed as Number(String(array_1)) | Number(String(array_2)).

And the iterator interface in JS is still several times slower than a simple loop over an array.

You may be able to get better performance by doing your math in , but I have no experience in how big the overhead is to "move" the data back and forth between JS and wasm.

Thomas
  • 11,958
  • 1
  • 14
  • 23
  • Thanks for the suggestion, ended up going down the rabbit hole of web assembly and it paid off massively in terms of read spedd (but took me a day to figure out how to do it!) ultimately led me to this answer: https://stackoverflow.com/a/65758597/12728698 – joshp Jan 17 '21 at 08:35