Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tvm
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cld
ml
tvm
Commits
6a3a9572
Commit
6a3a9572
authored
6 years ago
by
Sergei Grechanik
Committed by
Tianqi Chen
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[NNVM][TEST] Numgrad: fix nan and multioutput (#1754)
parent
06f91dd2
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
nnvm/python/nnvm/testing/check_computation.py
+45
-39
45 additions, 39 deletions
nnvm/python/nnvm/testing/check_computation.py
nnvm/tests/python/compiler/test_top_level1.py
+1
-0
1 addition, 0 deletions
nnvm/tests/python/compiler/test_top_level1.py
with
46 additions
and
39 deletions
nnvm/python/nnvm/testing/check_computation.py
+
45
−
39
View file @
6a3a9572
...
...
@@ -55,84 +55,84 @@ def infer_shapes_dtypes(graph, shape=None, dtype=None, fallback_dtype=None):
"""
# Preprocess input parameters
if
shape
is
None
:
shape
=
{}
provided_shapes
=
{}
elif
isinstance
(
shape
,
dict
):
provided_shapes
=
shape
else
:
provided_shapes
=
{
x
:
shape
for
x
in
graph
.
symbol
.
list_input_variables
()}
if
dtype
is
None
:
dtype
=
{}
if
not
isinstance
(
shape
,
dict
):
shape
=
{
x
:
shape
for
x
in
graph
.
symbol
.
list_input_variables
()}
if
not
isinstance
(
dtype
,
dict
):
dtype
=
{
x
:
dtype
for
x
in
graph
.
symbol
.
list_input_variables
()}
provided_dtypes
=
{}
elif
isinstance
(
dtype
,
dict
):
provided_dtypes
=
dtype
else
:
provided_dtypes
=
{
x
:
dtype
for
x
in
graph
.
symbol
.
list_input_variables
()}
shape
=
_dict_var_to_dict_str
(
shape
)
dtype
=
_dict_var_to_dict_str
(
dtype
)
provided_
shape
s
=
_dict_var_to_dict_str
(
provided_
shape
s
)
provided_
dtype
s
=
_dict_var_to_dict_str
(
provided_
dtype
s
)
# The graph may already contain shape and dtype info, so extract it and merge with
# the user-specified shapes and dtypes (use the user-specified one on contradiction)
all_initial
_shapes
=
graph
.
json_attr
(
'
shape
'
)
all_initial
_dtypes
=
graph
.
json_attr
(
'
dtype
'
)
preexisting
_shapes
=
graph
.
json_attr
(
'
shape
'
)
preexisting
_dtypes
=
graph
.
json_attr
(
'
dtype
'
)
if
all_initial
_shapes
:
if
preexisting
_shapes
:
for
x
in
graph
.
index
.
input_names
:
if
x
not
in
shape
:
x_shape
=
tuple
(
all_initial
_shapes
[
graph
.
index
.
entry_id
(
x
)])
shape
[
x
]
=
x_shape
if
x
not
in
provided_
shape
s
:
x_shape
=
tuple
(
preexisting
_shapes
[
graph
.
index
.
entry_id
(
x
)])
provided_
shape
s
[
x
]
=
x_shape
if
all_initial
_dtypes
:
if
preexisting
_dtypes
:
for
x
in
graph
.
index
.
input_names
:
if
x
not
in
dtype
:
x_dtype
=
TCODE_TO_DTYPE
[
all_initial
_dtypes
[
graph
.
index
.
entry_id
(
x
)]]
dtype
[
x
]
=
x_dtype
if
x
not
in
provided_
dtype
s
:
x_dtype
=
TCODE_TO_DTYPE
[
preexisting
_dtypes
[
graph
.
index
.
entry_id
(
x
)]]
provided_
dtype
s
[
x
]
=
x_dtype
# Perform inference
nnvm
.
compiler
.
graph_attr
.
set_shape_inputs
(
graph
,
shape
)
nnvm
.
compiler
.
graph_attr
.
set_dtype_inputs
(
graph
,
dtype
)
nnvm
.
compiler
.
graph_attr
.
set_shape_inputs
(
graph
,
provided_
shape
s
)
nnvm
.
compiler
.
graph_attr
.
set_dtype_inputs
(
graph
,
provided_
dtype
s
)
graph
=
graph
.
apply
(
'
InferShape
'
).
apply
(
'
InferType
'
)
shapes
=
graph
.
json_attr
(
'
shape
'
)
dtypes
=
graph
.
json_attr
(
'
dtype
'
)
out_len
=
len
(
graph
.
symbol
.
list_output_names
())
inferred_shapes
=
graph
.
json_attr
(
'
shape
'
)
inferred_dtypes
=
graph
.
json_attr
(
'
dtype
'
)
index
=
graph
.
index
output_shapes
=
\
[
tuple
(
shapes
[
index
.
entry
_id
(
index
.
output_entries
[
i
])])
for
i
in
range
(
out_len
)
]
output_dtypes
=
\
[
TCODE_TO_DTYPE
[
dtypes
[
index
.
entry
_id
(
index
.
output_entries
[
i
])]]
for
i
in
range
(
out_len
)
]
output_shapes
=
[
tuple
(
inferred_shapes
[
index
.
entry_id
(
entry
)])
for
entry
in
index
.
output_entries
]
output_dtypes
=
[
TCODE_TO_DTYPE
[
inferred_dtypes
[
index
.
entry_id
(
entry
)]]
for
entry
in
index
.
output_entries
]
# Postprocess the results
input_shapes
=
shape
.
copy
()
input_dtypes
=
dtype
.
copy
()
input_shapes
=
provided_
shape
s
.
copy
()
input_dtypes
=
provided_
dtype
s
.
copy
()
for
x
in
graph
.
symbol
.
list_input_variables
():
x_name
=
x
.
attr
(
'
name
'
)
x_
node
_id
=
graph
.
index
.
node
_id
(
x_name
)
input_shapes
[
x_name
]
=
tuple
(
shapes
[
x_
node
_id
])
input_dtypes
[
x_name
]
=
TCODE_TO_DTYPE
[
dtypes
[
x_
node
_id
]]
x_
entry
_id
=
graph
.
index
.
entry
_id
(
x_name
)
input_shapes
[
x_name
]
=
tuple
(
inferred_
shapes
[
x_
entry
_id
])
input_dtypes
[
x_name
]
=
TCODE_TO_DTYPE
[
inferred_
dtypes
[
x_
entry
_id
]]
# Merge the original user-specified shapes in case some of them are specified for non-existing
# variables
for
x_name
,
x_shape
in
shape
.
items
():
for
x_name
,
x_shape
in
provided_
shape
s
.
items
():
x_shape
=
tuple
(
x_shape
)
if
input_shapes
.
get
(
x_name
,
x_shape
)
!=
x_shape
:
raise
RuntimeError
(
"
Inferred shape differs from the provided shape.
\n
"
"
Provided shapes: {}
\n
Inferred shapes: {}
"
.
format
(
shapes
,
input_shapes
))
.
format
(
provided_
shapes
,
input_shapes
))
else
:
input_shapes
[
x_name
]
=
x_shape
# Merge the original user-specified dtypes
for
x_name
,
x_dtype
in
dtype
.
items
():
for
x_name
,
x_dtype
in
provided_
dtype
s
.
items
():
if
not
isinstance
(
x_dtype
,
str
):
x_dtype
=
TCODE_TO_DTYPE
[
x_dtype
]
if
input_dtypes
.
get
(
x_name
,
x_dtype
)
!=
x_dtype
:
raise
RuntimeError
(
"
Inferred dtype differs from the provided dtype.
\n
"
"
Provided dtypes: {}
\n
Inferred dtypes: {}
"
.
format
(
dtypes
,
input_dtypes
))
.
format
(
provided_
dtypes
,
input_dtypes
))
else
:
input_dtypes
[
x_name
]
=
x_dtype
...
...
@@ -622,6 +622,12 @@ def check_numerical_grads(function, input_values, grad_values, function_value=No
dist
=
np
.
sqrt
(
np
.
sum
((
ngrad
-
grad
)
**
2
))
grad_norm
=
np
.
sqrt
(
np
.
sum
(
ngrad
**
2
))
if
not
(
np
.
isfinite
(
dist
)
and
np
.
isfinite
(
grad_norm
)):
raise
ValueError
(
"
NaN or infinity detected during numerical gradient checking wrt {}
\n
"
"
analytical grad = {}
\n
numerical grad = {}
\n
"
.
format
(
x_name
,
grad
,
ngrad
))
# we multiple atol by this number to make it more universal for different sizes
sqrt_n
=
np
.
sqrt
(
float
(
np
.
prod
(
grad
.
shape
)))
...
...
This diff is collapsed.
Click to expand it.
nnvm/tests/python/compiler/test_top_level1.py
+
1
−
0
View file @
6a3a9572
...
...
@@ -96,6 +96,7 @@ def test_check_function():
_check_function_must_fail
(
sym
.
block_grad
(
x
+
2
*
y
),
numerical_grads
=
True
)
_check_function_must_fail
(
x
*
x
,
numerical_grads
=
True
,
numerical_grads_params
=
{
'
atol
'
:
0.0
,
'
rtol
'
:
0.0
})
_check_function_must_fail
(
sym
.
log
(
-
x
*
x
),
numerical_grads
=
True
,
error
=
ValueError
)
# different styles of returning results from the forward function
check_function
(
x
+
2
*
y
,
lambda
x
,
y
:
[
x
+
2
*
y
],
numerical_grads
=
False
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment