-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Infer dtype in SymbolBlock import from input symbol #12412
Changes from 7 commits
d69b7f6
f82344d
980c9e3
62acade
0c4e45b
f66f7a8
2033347
346632d
db83669
05287e5
f382183
fb6158a
0ff23f8
b58ea41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import os | ||
import tempfile | ||
|
||
import mxnet as mx | ||
from mxnet import gluon | ||
from mxnet.gluon import nn | ||
|
@@ -336,6 +339,29 @@ def hybrid_forward(self, F, x): | |
net.hybridize() | ||
assert isinstance(net(mx.nd.zeros((16, 10))), mx.nd.NDArray) | ||
|
||
# Test case to verify if initializing the SymbolBlock from a model with params | ||
# other than fp32 param dtype. | ||
|
||
# 1. Load a resnet model, cast it to fp64 and export | ||
tmp = tempfile.mkdtemp() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we delete the temporary directory when done with it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Temp gets automatically cleaned up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to python docs, it seems that it should be deleted by the user. The user of mkdtemp() is responsible for deleting the temporary directory and its contents when done with it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. Thanks. I meant, temp gets automatically cleaned up after all the tests (test session). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marcoabreu - Can you please confirm if my understanding is correct? If not, I will add code to delete the temp directory created in the tests. Also, I see similar behavior in all other tests, where it creates temp dir, but, assumes, it will be cleaned up the system. |
||
tmpfile = os.path.join(tmp, 'resnet34_fp64') | ||
ctx = mx.cpu(0) | ||
|
||
net_fp32 = mx.gluon.model_zoo.vision.resnet34_v2(pretrained=True, ctx=ctx, root=tmp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the model name in model zoo going to be maintained? If there is any change in the name, it would break this unit test. Not sure if we want to keep this dependency There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resnet34_v2 is a public function exposed through model_zoo.vision module. I think it is ok because we are not using string based selection of the model. |
||
net_fp32.cast('float64') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we add another test that casts to float16? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a test for fp16. |
||
net_fp32.hybridize() | ||
data = mx.nd.zeros((1,3,224,224), dtype='float64', ctx=ctx) | ||
net_fp32.forward(data) | ||
net_fp32.export(tmpfile, 0) | ||
|
||
# 2. Load the saved model and verify if all the params are loaded correctly. | ||
# and choose one of the param to verify the type if fp64. | ||
sm = mx.sym.load(tmpfile + '-symbol.json') | ||
inputs = mx.sym.var('data', dtype='float64') | ||
net_fp64 = mx.gluon.SymbolBlock(sm, inputs) | ||
net_fp64.collect_params().load(tmpfile + '-0000.params', ctx=ctx) | ||
assert (net_fp64.params['resnetv20_stage1_conv2_weight'].dtype is np.float64) | ||
|
||
@with_seed() | ||
@raises(AssertionError) | ||
def test_sparse_symbol_block(): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this does not handle Grouped Symbol because you are only slicing [0] from the symbol.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, I think the type inference should occur in
cast
as well, otherwise it's buggy when user is trying to cast dtype of a cascaded network with symbolBlock inside.