Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions tfjs-backend-wasm/src/cc/kernels/Gather.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ namespace {
template <typename T>
void gather_impl(const T* x_ptr, const std::vector<size_t>& x_strides,
const int32_t* indices_ptr, const size_t out_size,
const size_t batch_size,
const size_t indices_per_batch,
const std::vector<size_t>& out_strides, T* out_buf_ptr) {
for (size_t i = 0; i < out_size; ++i) {
auto loc = tfjs::util::offset_to_loc(i, out_strides);
const size_t batch_loc = loc[0];
const size_t indices_loc = loc[2];
loc[2] = indices_ptr[batch_loc * batch_size + indices_loc];
loc[2] = indices_ptr[batch_loc * indices_per_batch + indices_loc];

const size_t original_index = tfjs::util::loc_to_offset(loc, x_strides);

Expand All @@ -50,7 +50,7 @@ EMSCRIPTEN_KEEPALIVE

void Gather(const size_t x_id, const DType dtype, const int32_t* x_strides_ptr,
const size_t strides_size, const size_t indices_id,
const size_t batch_size, const int32_t* out_strides_ptr,
const size_t indices_per_batch, const int32_t* out_strides_ptr,
const size_t out_id) {
auto& x_info = backend::get_tensor_info(x_id);
auto& indices_info = backend::get_tensor_info(indices_id);
Expand All @@ -67,15 +67,15 @@ void Gather(const size_t x_id, const DType dtype, const int32_t* x_strides_ptr,
switch (dtype) {
case DType::float32:
gather_impl<float>(x_info.f32(), x_strides, indices_buf, out_size,
batch_size, out_strides, out_info.f32_write());
indices_per_batch, out_strides, out_info.f32_write());
break;
case DType::int32:
gather_impl<int32_t>(x_info.i32(), x_strides, indices_buf, out_size,
batch_size, out_strides, out_info.i32_write());
indices_per_batch, out_strides, out_info.i32_write());
break;
case DType::boolean:
gather_impl<bool>(x_info.b(), x_strides, indices_buf, out_size,
batch_size, out_strides, out_info.b_write());
indices_per_batch, out_strides, out_info.b_write());
break;
default:
util::warn("Gather for tensor id %d failed. Unknown dtype %d", x_id,
Expand Down
7 changes: 4 additions & 3 deletions tfjs-backend-wasm/src/kernels/GatherV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {CppDType} from './types';

let wasmGather: (
xId: number, dtype: CppDType, xStrides: Uint8Array, stridesSize: number,
indicesId: number, batchSize: number, outStrides: Uint8Array,
indicesId: number, indicesPerBatch: number, outStrides: Uint8Array,
outId: number) => void;

function setup(backend: BackendWasm): void {
Expand All @@ -34,7 +34,7 @@ function setup(backend: BackendWasm): void {
'array', // xStrides
'number', // stridesSize
'number', // indicesId
'number', // batchSize
'number', // indicesPerBatch
'array', // outStrides
'number' // outId
]);
Expand Down Expand Up @@ -88,6 +88,7 @@ function gatherV2(
return out;
}
const stridesSize = flattenX.shape.length - 1;
const indicesPerBatch = indicesSize / shapeInfo.batchSize;

const xData = backend.dataIdMap.get(flattenX.dataId);
const xId = xData.id;
Expand All @@ -104,7 +105,7 @@ function gatherV2(

wasmGather(
xId, CppDType[x.dtype], xStridesBytes, stridesSize, indicesId,
shapeInfo.batchSize, outStridesBytes, outId);
indicesPerBatch, outStridesBytes, outId);

backend.disposeData(flattenX.dataId);
backend.disposeData(flattenIndex.dataId);
Expand Down