torch.tx.withPoolScope
function withPoolScope<T>(pool: BufferPool, fn: () => T): TExecute a function with automatic buffer cleanup.
Any buffers allocated from the pool during the function execution will be returned to the pool when the function completes.
Parameters
poolBufferPool- The buffer pool to use
fn() => T- The function to execute
Returns
T– The result of the functionExamples
const pool = new BufferPool();
const result = withPoolScope(pool, () => {
const temp1 = pool.acquire([2, 3], 'float32', torch.empty);
const temp2 = pool.acquire([2, 3], 'float32', torch.empty);
// ... use temps ...
return finalResult;
});
// temp1 and temp2 are automatically returned to the pool