Skip to content
This repository was archived by the owner on Aug 15, 2019. It is now read-only.

Support WeChat mini app environment #1510

Merged
merged 12 commits into from
Jan 29, 2019
Merged

Support WeChat mini app environment #1510

merged 12 commits into from
Jan 29, 2019

Conversation

pyu10055
Copy link
Collaborator

@pyu10055 pyu10055 commented Jan 24, 2019

To compensate the differences between browser and WeChat mini app:

  • WeChat mini app runs on JS core (ios) which does not have document, window, and setImmediate function or objects.
  • When creating a GPGUContext with a existing context, it needs to store the context for the GL version, otherwise it would be picked later.
  • expose setTensorTracker function to allow external backend registration to use.

This PR also fix the inconsistency issue with GPGPUContext constructor, it should always cache the rendering context.


This change is Reviewable

@pyu10055 pyu10055 requested a review from dsmilkov January 24, 2019 22:59
Copy link
Contributor

@nsthorat nsthorat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need most of this code. You can already register a custom backend:

tf.ENV.registerBackend('wechat', () => new WeChatBackend());

We could expose registerBackend on the top-level tf.

As for the custom WebGL Context, the WeChatBackend should extend the WebGLBackend. The constructor allows passing a GPGPUContext:
https://github.com/tensorflow/tfjs-core/blob/master/src/kernels/backend_webgl.ts#L563

The GPGPUContext allows passing a WebGL Context.

So I think we have all the public facing API we need to do this.

Am I missing something?

Reviewed 1 of 7 files at r1.
Reviewable status: 0 of 1 approvals obtained (waiting on @dsmilkov and @pyu10055)


yarn.lock, line 8 at r1 (raw file):

  version "0.0.38"
  resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.38.tgz#c1be40aa933723c608820a99a373a16d215a1ca2"
  integrity sha512-F/v7t1LwS4vnXuPooJQGBRKRGIoxWUTmA4VHfqjOccFsNDThD5bfUNpITive6s352O7o384wcpEaDV8rHCehDA==

please revert this file, you have an old yarn (you should update it! this has happened a couple times :) )


src/browser_util.ts, line 18 at r1 (raw file):

 */

const delayCallback: Function = typeof requestAnimationFrame !== 'undefined' ?

this is getting pretty hard to parse now, how about:

const delayCallback: Function;
  if (...) {
    delayCallback = ...
  } else if (...) {
  }
}

Copy link
Contributor

@dsmilkov dsmilkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 7 files at r1.
Reviewable status: 0 of 1 approvals obtained (waiting on @dsmilkov and @pyu10055)


src/environment.ts, line 466 at r1 (raw file):

}

export function getGlobalNamespace(): {ENV: Environment} {

why do you need to export this method?


src/index.ts, line 47 at r1 (raw file):

export {RMSPropOptimizer} from './optimizers/rmsprop_optimizer';
export {SGDOptimizer} from './optimizers/sgd_optimizer';
export {Scalar, setTensorTracker, Tensor, Tensor1D, Tensor2D, Tensor3D, Tensor4D, TensorBuffer, variable, Variable} from './tensor';

you shouldn't expose this. just make a new custom backend and set it.


src/kernels/webgl/gpgpu_context.ts, line 54 at r1 (raw file):

    if (gl != null) {
      this.gl = gl;
      setWebGLContext(glVersion, gl);

instead of calling it here, expose this method as tf.webgl.setWebGLContext() and call it from the wechat repo. Also make a custom WeChat backend that extends from the WebGL backend.

Copy link
Collaborator Author

@pyu10055 pyu10055 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is an inconsistency in the GPGPUContext constructor, when the rendering gl context is not provided, an default context is created automatically, and cached in its local GL_VERSION => context map, but if a external gl context is provided the caching is not done, which causes issues later.
I think this is a bug, and the setWebglContext method should not be exposed to custom backend, since it is an internal implementation details that custom backend should not need to know.

Reviewable status: 0 of 1 approvals obtained (waiting on @dsmilkov)


src/environment.ts, line 466 at r1 (raw file):

Previously, dsmilkov (Daniel Smilkov) wrote…

why do you need to export this method?

Currently other places also are doing the same logic to find the exposed symbol, for example fetch for ioHandler. By exposing this, we can centralize this search logic and potential caching the namespace.


src/index.ts, line 47 at r1 (raw file):

Previously, dsmilkov (Daniel Smilkov) wrote…

you shouldn't expose this. just make a new custom backend and set it.

Without exposing this, how can a custom backend access the internal variable trackFn in the tensor.ts file?


src/kernels/webgl/gpgpu_context.ts, line 54 at r1 (raw file):

Previously, dsmilkov (Daniel Smilkov) wrote…

instead of calling it here, expose this method as tf.webgl.setWebGLContext() and call it from the wechat repo. Also make a custom WeChat backend that extends from the WebGL backend.

as I stated in the response to Nikhil, this is an internal details of how webgl backend looks up context, custom backend should not need to worry about.

Copy link
Collaborator Author

@pyu10055 pyu10055 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this can be done with an external backend, but I think it should be fixed instead of pushing out.

Reviewable status: 0 of 1 approvals obtained (waiting on @dsmilkov)

Copy link
Contributor

@dsmilkov dsmilkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey can you point us to the WeChat repo. Would be good to see the code there and which flags you are overriding.

Reviewed 3 of 7 files at r1, 2 of 3 files at r2.
Reviewable status: 0 of 1 approvals obtained (waiting on @pyu10055)


src/environment.ts, line 466 at r1 (raw file):

Previously, pyu10055 (Ping Yu) wrote…

Currently other places also are doing the same logic to find the exposed symbol, for example fetch for ioHandler. By exposing this, we can centralize this search logic and potential caching the namespace.

Since we are making this a public API, let's add this as a property global to the Environment class, so users can do tf.ENV.global to access it.


src/index.ts, line 47 at r1 (raw file):

Previously, pyu10055 (Ping Yu) wrote…

Without exposing this, how can a custom backend access the internal variable trackFn in the tensor.ts file?

You won't need this after this PR is in: #1517 so remove the export.


src/kernels/webgl/gpgpu_context.ts, line 54 at r1 (raw file):

Previously, pyu10055 (Ping Yu) wrote…

as I stated in the response to Nikhil, this is an internal details of how webgl backend looks up context, custom backend should not need to worry about.

sounds good. let's do it this way for now.

Copy link
Collaborator Author

@pyu10055 pyu10055 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tf.ENV.set('WEBGL_VERSION', 1);
and for iOS:
tf.ENV.set('WEBGL_RENDER_FLOAT32_ENABLED', false);

Reviewable status: 0 of 1 approvals obtained


src/environment.ts, line 466 at r1 (raw file):

Previously, dsmilkov (Daniel Smilkov) wrote…

Since we are making this a public API, let's add this as a property global to the Environment class, so users can do tf.ENV.global to access it.

adding the global inside the Environment object would cause recursive reference, which might be issue for logging. I added a caching at the file level.

Copy link
Contributor

@dsmilkov dsmilkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 1 approvals obtained (waiting on @pyu10055)


src/environment.ts, line 466 at r1 (raw file):

Previously, pyu10055 (Ping Yu) wrote…

adding the global inside the Environment object would cause recursive reference, which might be issue for logging. I added a caching at the file level.

Sorry i wasn't clear. Add a getter to the Environment class and don't export getGlobalNamespace:

  get global() {
    return getGlobalNamespace();
  }

This way users can access tf.ENV.global which points them to window/process/global depending on the environment they are in.

Copy link
Collaborator Author

@pyu10055 pyu10055 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 1 approvals obtained (waiting on @dsmilkov)


src/environment.ts, line 466 at r1 (raw file):

Previously, dsmilkov (Daniel Smilkov) wrote…

Sorry i wasn't clear. Add a getter to the Environment class and don't export getGlobalNamespace:

  get global() {
    return getGlobalNamespace();
  }

This way users can access tf.ENV.global which points them to window/process/global depending on the environment they are in.

Done.

Copy link
Contributor

@dsmilkov dsmilkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 1 approvals obtained (waiting on @pyu10055)


src/environment.ts, line 466 at r1 (raw file):

Previously, pyu10055 (Ping Yu) wrote…

Done.

Remove the _global variable since you don't really need it.

@pyu10055
Copy link
Collaborator Author

Reviewable status: 0 of 1 approvals obtained (waiting on @pyu10055)

src/environment.ts, line 466 at r1 (raw file):

Previously, pyu10055 (Ping Yu) wrote…
Remove the _global variable since you don't really need it.
This cache speeds up the lookup, since now this method can be called multiple times.

Copy link
Contributor

@dsmilkov dsmilkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 1 approvals obtained


src/environment.ts, line 466 at r1 (raw file):

Previously, dsmilkov (Daniel Smilkov) wrote…

Remove the _global variable since you don't really need it.

LGTM

@dsmilkov dsmilkov merged commit c211b49 into master Jan 29, 2019
@nsthorat nsthorat deleted the wechat2 branch February 5, 2019 16:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants