Skip to content

Commit da219cd

Browse files
committed
refactor(embeddings): pre-allocate static tensors via as const array
Align the text/image embeddings tasks with the add-task-pipeline skill (and every other task): allocate the static output tensor in a `[...] as const` array, destructure it, and dispose via `tensors.forEach`.
1 parent 6c3ccc4 commit da219cd

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

packages/react-native-executorch/src/extensions/cv/tasks/imageEmbeddings.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ export async function createImageEmbeddings(
6565
const inpShape = meta.inputTensorMeta[0]!.shape;
6666
const outShape = meta.outputTensorMeta[0]!.shape;
6767

68-
const tEmbedding = tensor('float32', outShape);
68+
const tensors = [tensor('float32', outShape)] as const;
69+
const [tEmbedding] = tensors;
6970
const preprocessor = createImagePreprocessor(opts, inpShape);
7071

7172
const dispose = () => {
7273
preprocessor.dispose();
73-
tEmbedding.dispose();
74+
tensors.forEach((t) => t.dispose());
7475
model.dispose();
7576
};
7677

packages/react-native-executorch/src/extensions/nlp/tasks/textEmbeddings.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ export async function createTextEmbeddings(
7474
const maxSeqLen = meta.inputTensorMeta[0]!.shape[1]!;
7575
const outShape = meta.outputTensorMeta[0]!.shape;
7676

77-
const tEmbedding = tensor('float32', outShape);
77+
const tensors = [tensor('float32', outShape)] as const;
78+
const [tEmbedding] = tensors;
7879

7980
const dispose = () => {
80-
tEmbedding.dispose();
81+
tensors.forEach((t) => t.dispose());
8182
tokenizer.dispose();
8283
model.dispose();
8384
};

0 commit comments

Comments
 (0)