facebookresearch / playtorch

PlayTorch is a framework for rapidly creating mobile AI experiences.
https://playtorch.dev/
MIT License
828 stars 102 forks source link

torch.jit._load_for_mobile with device and extra files options #141

Closed raedle closed 1 year ago

raedle commented 2 years ago

Summary

The PlayTorch API for torch.jit._loadForMobile did not support arguments for torch device and extra files. This change updates the torch.jit._loadForMobile API to also allow for both parameters. The extra files are useful to store additional data with the model (e.g., store image classes for inference alongside the model).

Changelog

Test Plan

Example:

let modelUrl = await MobileModel.download(
  require('../../assets/models/dummy_test_model.ptl'),
);

const extraFiles = {foo: undefined, 'model/classes.json': undefined, 'classes.json': undefined};
const model = await torch.jit._loadForMobile<TestModule>(modelUrl, 'cpu', extraFiles);
let lastTime = performance.now();

for (let i = 0; i < 1000; i++) {
  await model.bump(1);
}
let totalTime = performance.now() - lastTime;
console.log('average time for bump: ', totalTime / 1000);
console.log(model);
console.log('extraFiles', extraFiles);

Output:

 LOG  {"forward": [Function forward], "forwardSync": [Function forwardSync]}
 LOG  extraFiles {"classes.json": "", "foo": "", "model/classes.json": "[
  \"Arizona Bark Scorpion\",
  \"Giant Hairy Desert Scorpion\",
  \"Giant Whip Scorpion\",
  \"Northern Scorpion\",
  \"Pseudoscorpiones\",
  \"Stripe-tailed Scorpion\",
  \"Tailless Whip Scorpion\"
]
"}
 LOG  pi:  3.14
 LOG  piSync:  3.14
vercel[bot] commented 2 years ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
playtorch ✅ Ready (Inspect) Visit Preview Nov 4, 2022 at 11:31PM (UTC)
facebook-github-bot commented 1 year ago

@raedle has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

facebook-github-bot commented 1 year ago

@raedle has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

facebook-github-bot commented 1 year ago

@raedle has updated the pull request. You must reimport the pull request before landing.

facebook-github-bot commented 1 year ago

@raedle has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

facebook-github-bot commented 1 year ago

This pull request was exported from Phabricator. Differential Revision: D41017647

facebook-github-bot commented 1 year ago

This pull request was exported from Phabricator. Differential Revision: D41017647

facebook-github-bot commented 1 year ago

This pull request was exported from Phabricator. Differential Revision: D41017647