FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tensorflow/tensorflow/python/eager/core_test.py at master · codeplaysoftware/tensorflow · GitHub
codeplaysoftware
tensorflow
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tensorflow
/
tensorflow
/
python
/
eager
/
core_test.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
672 lines (580 loc) · 23.3 KB
Breadcrumbs
tensorflow
/
tensorflow
/
python
/
eager
/
core_test.py
Copy path
File metadata and controls
672 lines (580 loc) · 23.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for core."""
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
threading
import
numpy
as
np
from
tensorflow
.
core
.
protobuf
import
config_pb2
from
tensorflow
.
python
import
pywrap_tensorflow
from
tensorflow
.
python
.
eager
import
context
from
tensorflow
.
python
.
eager
import
core
from
tensorflow
.
python
.
eager
import
execute
as
execute_lib
from
tensorflow
.
python
.
eager
import
test
from
tensorflow
.
python
.
framework
import
constant_op
from
tensorflow
.
python
.
framework
import
dtypes
from
tensorflow
.
python
.
framework
import
errors
from
tensorflow
.
python
.
framework
import
ops
from
tensorflow
.
python
.
framework
import
test_util
from
tensorflow
.
python
.
ops
import
array_ops
from
tensorflow
.
python
.
ops
import
gen_resource_variable_ops
from
tensorflow
.
python
.
ops
import
nn_ops
from
tensorflow
.
python
.
ops
import
resource_variable_ops
def
execute
(
op_name
,
num_outputs
,
inputs
,
attrs
=
None
):
return
execute_lib
.
execute
(
op_name
,
num_outputs
,
inputs
,
attrs
,
context
.
context
())
def
truncated_normal
(
shape
):
return
execute
(
b'TruncatedNormal'
,
1
,
inputs
=
[
shape
],
attrs
=
(
'dtype'
,
dtypes
.
float32
.
as_datatype_enum
,
'T'
,
shape
.
dtype
.
as_datatype_enum
,
'seed'
,
0
,
'seed2'
,
0
))[
0
]
class
TFETest
(
test_util
.
TensorFlowTestCase
):
def
testContext
(
self
):
ctx
=
context
.
Context
()
self
.
assertTrue
(
ctx
.
executing_eagerly
())
self
.
assertEqual
(
''
,
ctx
.
scope_name
)
ctx
.
scope_name
=
'foo'
self
.
assertEqual
(
'foo'
,
ctx
.
scope_name
)
self
.
assertEqual
(
context
.
SYNC
,
ctx
.
get_execution_mode
())
ctx
.
set_execution_mode
(
context
.
ASYNC
)
self
.
assertEqual
(
context
.
ASYNC
,
ctx
.
get_execution_mode
())
ctx
.
set_execution_mode
(
context
.
SYNC
)
self
.
assertEqual
(
context
.
SYNC
,
ctx
.
get_execution_mode
())
with
ctx
.
execution_mode
(
context
.
ASYNC
):
self
.
assertEqual
(
context
.
ASYNC
,
ctx
.
get_execution_mode
())
ctx
.
set_execution_mode
(
context
.
SYNC
)
self
.
assertEqual
(
context
.
SYNC
,
ctx
.
get_execution_mode
())
self
.
assertIsNone
(
ctx
.
summary_writer_resource
)
ctx
.
summary_writer_resource
=
'mock'
self
.
assertEqual
(
'mock'
,
ctx
.
summary_writer_resource
)
self
.
assertEqual
(
''
,
ctx
.
device_name
)
self
.
assertEqual
(
ctx
.
device_name
,
ctx
.
device_spec
.
to_string
())
with
ctx
.
device
(
'GPU:0'
):
self
.
assertEqual
(
'/job:localhost/replica:0/task:0/device:GPU:0'
,
ctx
.
device_name
)
self
.
assertEqual
(
ctx
.
device_name
,
ctx
.
device_spec
.
to_string
())
with
ctx
.
device
(
None
):
self
.
assertEqual
(
''
,
ctx
.
device_name
)
self
.
assertEqual
(
ctx
.
device_name
,
ctx
.
device_spec
.
to_string
())
with
ctx
.
device
(
'CPU:0'
):
self
.
assertEqual
(
'/job:localhost/replica:0/task:0/device:CPU:0'
,
ctx
.
device_name
)
self
.
assertEqual
(
ctx
.
device_name
,
ctx
.
device_spec
.
to_string
())
has_cpu_device
=
False
for
x
in
ctx
.
devices
():
has_cpu_device
=
has_cpu_device
or
'CPU'
in
x
self
.
assertTrue
(
has_cpu_device
)
del
ctx
def
testAsyncBasic
(
self
):
ctx
=
context
.
Context
(
execution_mode
=
context
.
ASYNC
)
has_cpu_device
=
False
for
x
in
ctx
.
devices
():
has_cpu_device
=
has_cpu_device
or
'CPU'
in
x
self
.
assertTrue
(
has_cpu_device
)
del
ctx
def
testRunMetadata
(
self
):
context
.
enable_run_metadata
()
t
=
constant_op
.
constant
(
1.0
)
_
=
t
+
t
# Runs an operation which will be in the RunMetadata
run_metadata
=
context
.
export_run_metadata
()
context
.
disable_run_metadata
()
step_stats
=
run_metadata
.
step_stats
self
.
assertGreater
(
len
(
step_stats
.
dev_stats
),
0
)
cpu_stats
=
step_stats
.
dev_stats
[
0
]
self
.
assertEqual
(
'/job:localhost/replica:0/task:0/device:CPU:0'
,
cpu_stats
.
device
)
self
.
assertGreaterEqual
(
len
(
cpu_stats
.
node_stats
),
1
)
def
testShouldCopy
(
self
):
if
not
context
.
context
().
num_gpus
():
self
.
skipTest
(
'No devices other than CPUs found'
)
with
ops
.
device
(
'gpu:0'
):
x
=
constant_op
.
constant
(
1.0
)
y
=
array_ops
.
identity
(
x
)
# The value we're testing y.device against will depend on what the behavior
# of not explicitly specifying a device in the context is. This behavior is
# subject to change (for example, in the future we may want to use GPUs, if
# available, when no device is explicitly provided)
self
.
assertEqual
(
y
.
device
,
'/job:localhost/replica:0/task:0/device:CPU:0'
)
def
testContextSwitchStackContainsEagerMode
(
self
):
# Eager execution has been enabled, and no other context switch has
# occurred, so `context_switches` should contain exactly one entry.
self
.
assertEqual
(
len
(
context
.
context
().
context_switches
.
stack
),
1
)
switch
=
context
.
context
().
context_switches
.
stack
[
0
]
# The entry should log that eager mode was entered.
self
.
assertIs
(
switch
.
enter_context_fn
,
context
.
eager_mode
)
# It is not possible to build a graph function when eager execution
# is enabled; the stack entry should reflect this fact.
self
.
assertFalse
(
switch
.
is_building_function
)
def
testInt32GPU
(
self
):
if
not
context
.
context
().
num_gpus
():
self
.
skipTest
(
'No GPUs found'
)
with
ops
.
device
(
'gpu:0'
):
xent
=
nn_ops
.
sparse_softmax_cross_entropy_with_logits
(
logits
=
[[
0.0
,
0.0
]],
labels
=
[
0
])
self
.
assertAllClose
(
xent
, [
0.69314718
])
def
_runInThread
(
self
,
target
,
args
):
t
=
threading
.
Thread
(
target
=
target
,
args
=
args
)
try
:
t
.
start
()
t
.
join
()
except
Exception
as
e
:
raise
e
# Test that different thread local values are initialized to the same values
# in different threads.
def
testContextThreadLocalMembers
(
self
):
def
get_context_values
(
ctx
):
return
[
ctx
.
executing_eagerly
(),
ctx
.
scope_name
,
ctx
.
summary_writer_resource
,
ctx
.
device_name
,
ctx
.
num_gpus
()
]
def
get_values
(
ctx
,
values
):
values
.
extend
(
get_context_values
(
ctx
))
context_values
=
[]
ctx
=
context
.
Context
()
self
.
_runInThread
(
get_values
, (
ctx
,
context_values
))
self
.
assertAllEqual
(
context_values
,
get_context_values
(
ctx
))
def
testContextConfig
(
self
):
if
not
context
.
context
().
num_gpus
():
self
.
skipTest
(
'No GPUs found'
)
ctx
=
context
.
Context
(
config
=
config_pb2
.
ConfigProto
(
device_count
=
{
'GPU'
:
0
}))
self
.
assertEquals
(
0
,
ctx
.
num_gpus
())
def
testTensorPlacement
(
self
):
if
not
context
.
context
().
num_gpus
():
self
.
skipTest
(
'No GPUs found'
)
x
=
constant_op
.
constant
(
1.
).
gpu
()
with
context
.
device
(
'gpu:0'
):
y
=
constant_op
.
constant
(
2.
)
# Add would fail if t2 were not on GPU
result
=
execute
(
b'Add'
,
1
,
inputs
=
[
x
,
y
],
attrs
=
(
'T'
,
x
.
dtype
.
as_datatype_enum
))[
0
].
cpu
().
numpy
()
self
.
assertEqual
(
3
,
result
)
def
testResourceTensorPlacement
(
self
):
if
not
context
.
context
().
num_gpus
():
self
.
skipTest
(
'No GPUs found'
)
with
context
.
device
(
'gpu:0'
):
v
=
resource_variable_ops
.
ResourceVariable
(
1.0
)
with
context
.
device
(
'cpu:0'
):
# Check that even though we specified the cpu device we'll run the read op
# in the device where the handle is.
self
.
assertAllEqual
(
gen_resource_variable_ops
.
read_variable_op
(
v
.
handle
,
v
.
dtype
),
1.0
)
def
testCopyBetweenDevices
(
self
):
if
not
context
.
context
().
num_gpus
():
self
.
skipTest
(
'No GPUs found'
)
x
=
constant_op
.
constant
([[
1.
,
2.
], [
3.
,
4.
]])
x
=
x
.
cpu
()
x
=
x
.
gpu
()
x
=
x
.
gpu
()
x
=
x
.
cpu
()
# Invalid device
with
self
.
assertRaises
(
RuntimeError
):
x
.
gpu
(
context
.
context
().
num_gpus
()
+
1
)
def
testCopyBetweenDevicesAsync
(
self
):
if
not
context
.
context
().
num_gpus
():
self
.
skipTest
(
'No GPUs found'
)
with
context
.
execution_mode
(
context
.
ASYNC
):
x
=
constant_op
.
constant
([[
1.
,
2.
], [
3.
,
4.
]])
x
=
x
.
cpu
()
x
=
x
.
gpu
()
x
=
x
.
gpu
()
x
=
x
.
cpu
()
context
.
async_wait
()
# Invalid device
with
self
.
assertRaises
(
RuntimeError
):
x
.
gpu
(
context
.
context
().
num_gpus
()
+
1
)
context
.
async_wait
()
context
.
async_clear_error
()
def
testCopyScope
(
self
):
if
not
context
.
context
().
num_gpus
():
self
.
skipTest
(
'No GPUs found'
)
constant
=
constant_op
.
constant
(
1.0
)
with
ops
.
device
(
'gpu:0'
):
with
context
.
context
().
device_policy
(
context
.
DEVICE_PLACEMENT_SILENT
):
c
=
constant
+
1.0
self
.
assertAllEqual
(
c
,
2.0
)
def
testNumpyForceCPU
(
self
):
if
not
context
.
context
().
num_gpus
():
self
.
skipTest
(
'No GPUs found'
)
cpu
=
constant_op
.
constant
([[
1.
,
2.
], [
3.
,
4.
]])
c2g
=
cpu
.
gpu
()
self
.
assertAllEqual
(
c2g
,
cpu
.
numpy
())
def
testCopyFromCPUToCPU
(
self
):
ta
=
constant_op
.
constant
([[
1
,
2
], [
3
,
4
]])
tb
=
ta
.
cpu
()
self
.
assertNotEqual
(
id
(
ta
),
id
(
tb
))
self
.
assertAllEqual
(
ta
,
tb
.
numpy
())
def
testRegisterExceptionClass
(
self
):
with
self
.
assertRaises
(
TypeError
):
pywrap_tensorflow
.
TFE_Py_RegisterExceptionClass
(
str
)
pywrap_tensorflow
.
TFE_Py_RegisterExceptionClass
(
core
.
_NotOkStatusException
)
# pylint: disable=protected-access
# TODO(agarwal): add tests passing incorrect typed values to attrs.
def
testExecuteBasic
(
self
):
three
=
constant_op
.
constant
(
3
)
five
=
constant_op
.
constant
(
5
)
product
=
execute
(
b'Mul'
,
num_outputs
=
1
,
inputs
=
[
three
,
five
],
attrs
=
(
'T'
,
three
.
dtype
.
as_datatype_enum
))[
0
]
self
.
assertAllEqual
(
15
,
product
)
def
testExecuteBasicAsync
(
self
):
with
context
.
execution_mode
(
context
.
ASYNC
):
three
=
constant_op
.
constant
(
3
)
five
=
constant_op
.
constant
(
5
)
product
=
execute
(
b'Mul'
,
num_outputs
=
1
,
inputs
=
[
three
,
five
],
attrs
=
(
'T'
,
three
.
dtype
.
as_datatype_enum
))[
0
]
self
.
assertAllEqual
(
15
,
product
)
# Error: Invalid arguments
context
.
set_execution_mode
(
context
.
ASYNC
)
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'MatMul'
,
num_outputs
=
1
,
inputs
=
[
three
,
five
],
attrs
=
(
'transpose_a'
,
False
,
'transpose_b'
,
False
,
'T'
,
three
.
dtype
.
as_datatype_enum
))
context
.
async_wait
()
context
.
async_clear_error
()
context
.
set_execution_mode
(
context
.
SYNC
)
def
testExecuteTooManyNumOutputs
(
self
):
# num_outputs provided is 50, but only one output is produced.
product
=
execute
(
b'Mul'
,
num_outputs
=
50
,
inputs
=
[
constant_op
.
constant
(
3
),
constant_op
.
constant
(
5
)],
attrs
=
(
'T'
,
dtypes
.
int32
.
as_datatype_enum
))[
0
]
self
.
assertAllEqual
(
15
,
product
)
def
testExecuteTooFewNumOutputs
(
self
):
# num_outputs provided is 0, but one output is produced.
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
_
=
execute
(
b'Mul'
,
num_outputs
=
0
,
inputs
=
[
constant_op
.
constant
(
3
),
constant_op
.
constant
(
5
)],
attrs
=
(
'T'
,
dtypes
.
int32
.
as_datatype_enum
))[
0
]
def
testMatMulGPU
(
self
):
if
not
context
.
context
().
num_gpus
():
self
.
skipTest
(
'No GPUs found'
)
three
=
constant_op
.
constant
([[
3.
]]).
gpu
()
five
=
constant_op
.
constant
([[
5.
]]).
gpu
()
product
=
execute
(
b'MatMul'
,
num_outputs
=
1
,
inputs
=
[
three
,
five
],
attrs
=
(
'transpose_a'
,
False
,
'transpose_b'
,
False
,
'T'
,
three
.
dtype
.
as_datatype_enum
))[
0
]
self
.
assertAllEqual
([[
15.0
]],
product
)
def
testExecuteStringAttr
(
self
):
checked_three
=
execute
(
b'CheckNumerics'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
(
3.
)],
attrs
=
(
'message'
,
'just checking'
,
'T'
,
dtypes
.
float32
.
as_datatype_enum
))[
0
]
self
.
assertEqual
([[
3
]],
checked_three
.
numpy
())
def
testExecuteStringAttrBadValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
_
=
execute
(
b'CheckNumerics'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
(
3.
)],
attrs
=
(
'message'
,
1
,
'T'
,
dtypes
.
float32
.
as_datatype_enum
))
def
testExecuteFloatAttr
(
self
):
almost_equal
=
execute
(
b'ApproximateEqual'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
(
3.0
),
constant_op
.
constant
(
2.9
)],
attrs
=
(
'tolerance'
,
0.3
,
'T'
,
dtypes
.
float32
.
as_datatype_enum
))[
0
]
self
.
assertTrue
(
almost_equal
)
def
testExecuteFloatAttrBadValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
_
=
execute
(
b'ApproximateEqual'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
(
3.0
),
constant_op
.
constant
(
2.9
)],
attrs
=
(
'tolerance'
,
'0.3'
,
'T'
,
dtypes
.
float32
.
as_datatype_enum
))
def
testExecuteIntAttr
(
self
):
total
=
execute
(
b'AddN'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
(
3
),
constant_op
.
constant
(
4
)],
attrs
=
(
'T'
,
dtypes
.
int32
.
as_datatype_enum
,
'N'
,
2
))[
0
]
self
.
assertAllEqual
(
7
,
total
)
def
testExecuteIntAttrBadValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
_
=
execute
(
b'AddN'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
(
3
),
constant_op
.
constant
(
4
)],
attrs
=
(
'T'
,
dtypes
.
int32
.
as_datatype_enum
,
'N'
,
'2'
))
# Looks like we don't have an existing op with list(bool) attrs.
def
testExecuteBoolAttr
(
self
):
product
=
execute
(
b'MatMul'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
([[
3
]]),
constant_op
.
constant
([[
5
]])],
attrs
=
(
'transpose_a'
,
True
,
'transpose_b'
,
False
,
'T'
,
dtypes
.
int32
.
as_datatype_enum
))[
0
]
self
.
assertAllEqual
([[
15
]],
product
)
def
testExecuteShapeAttr
(
self
):
execute
(
b'VarHandleOp'
,
num_outputs
=
1
,
inputs
=
[],
attrs
=
(
'shape'
, [
1
,
2
],
'dtype'
,
dtypes
.
int32
.
as_datatype_enum
,
'container'
,
''
,
'shared_name'
,
''
))
def
testExecuteShapeAttrBadValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'VarHandleOp'
,
num_outputs
=
1
,
inputs
=
[],
attrs
=
(
'shape'
,
1
,
'dtype'
,
dtypes
.
int32
.
as_datatype_enum
,
'container'
,
''
,
'shared_name'
,
''
))
def
testExecuteListStringAttr
(
self
):
execute
(
b'TensorSummary'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
(
3.0
)],
attrs
=
(
'T'
,
dtypes
.
float32
.
as_datatype_enum
,
'description'
,
'tensor_summary'
,
'labels'
, [
'3'
,
'summary'
],
'display_name'
,
'test'
))
def
testExecuteListStringAttrBadValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'TensorSummary'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
(
3.0
)],
attrs
=
(
'T'
,
dtypes
.
float32
.
as_datatype_enum
,
'description'
,
''
,
'labels'
,
3
,
'display_name'
,
'test'
))
def
testExecuteListStringAttrBadListValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'TensorSummary'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
(
3.0
)],
attrs
=
(
'T'
,
dtypes
.
float32
.
as_datatype_enum
,
'description'
,
''
,
'labels'
, [
3
],
'display_name'
,
'test'
))
def
testExecuteListFloatAttr
(
self
):
b
=
execute
(
b'Bucketize'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
([
3.0
,
5.0
,
7.0
])],
attrs
=
(
'T'
,
dtypes
.
float32
.
as_datatype_enum
,
'boundaries'
, [
4.0
,
6.0
]))[
0
]
self
.
assertAllEqual
([
0
,
1
,
2
],
b
)
def
testExecuteListFloatAttrBadValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'Bucketize'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
([
3.0
,
5.0
,
7.0
])],
attrs
=
(
'T'
,
dtypes
.
float32
.
as_datatype_enum
,
'boundaries'
,
4.0
))
def
testExecuteListFloatAttrBadListValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'Bucketize'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
([
3.0
,
5.0
,
7.0
])],
attrs
=
(
'T'
,
dtypes
.
float32
.
as_datatype_enum
,
'boundaries'
,
[
'4.0'
,
'6.0'
]))
def
testExecuteListIntAttr
(
self
):
b
=
execute
(
b'Squeeze'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
([[[
3.0
]]])],
attrs
=
(
'T'
,
dtypes
.
float32
.
as_datatype_enum
,
'squeeze_dims'
, [
0
,
2
]))[
0
]
self
.
assertAllEqual
([
3
],
b
)
def
testExecuteListIntAttrBadValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'Squeeze'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
([[[
3.0
]]])],
attrs
=
(
'T'
,
dtypes
.
float32
.
as_datatype_enum
,
'squeeze_dims'
,
0
))
def
testExecuteListIntAttrBadListValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'Squeeze'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
([[[
3.0
]]])],
attrs
=
(
'T'
,
dtypes
.
float32
.
as_datatype_enum
,
'squeeze_dims'
,
[
'0'
,
'2'
]))
def
testExecuteListTypeListShapeAttr
(
self
):
execute
(
b'Barrier'
,
num_outputs
=
1
,
inputs
=
[],
attrs
=
(
'component_types'
, [
dtypes
.
float64
.
as_datatype_enum
],
'shapes'
,
[[
1
,
2
]],
'capacity'
,
-
1
,
'container'
,
''
,
'shared_name'
,
''
))
def
testExecuteListTypeAttrBadValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'Barrier'
,
num_outputs
=
1
,
inputs
=
[],
attrs
=
(
'component_types'
,
dtypes
.
float64
.
as_datatype_enum
,
'shapes'
,
[[
1
,
2
]],
'capacity'
,
-
1
,
'container'
,
''
,
'shared_name'
,
''
))
def
testExecuteListTypeAttrBadListValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'Barrier'
,
num_outputs
=
1
,
inputs
=
[],
attrs
=
(
'component_types'
,
'1'
,
'shapes'
, [[
1
,
2
]],
'capacity'
,
-
1
,
'container'
,
''
,
'shared_name'
,
''
))
def
testExecuteListShapeAttrBadValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'Barrier'
,
num_outputs
=
1
,
inputs
=
[],
attrs
=
(
'component_types'
, [
dtypes
.
float64
.
as_datatype_enum
],
'shapes'
,
[
1
,
2
],
'capacity'
,
-
1
,
'container'
,
''
,
'shared_name'
,
''
))
def
testExecuteListShapeAttrBadListValue
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'Barrier'
,
num_outputs
=
1
,
inputs
=
[],
attrs
=
(
'component_types'
, [
dtypes
.
float64
.
as_datatype_enum
],
'shapes'
,
[
1
],
'capacity'
,
-
1
,
'container'
,
''
,
'shared_name'
,
''
))
def
testExecuteMultipleOutputs
(
self
):
split_dim
=
1
value
=
[[
0
,
1
,
2
], [
3
,
4
,
5
]]
x1
,
x2
,
x3
=
execute
(
b'Split'
,
num_outputs
=
3
,
inputs
=
[
constant_op
.
constant
(
split_dim
),
constant_op
.
constant
(
value
)],
attrs
=
(
'num_split'
,
3
,
'T'
,
dtypes
.
int32
.
as_datatype_enum
))
self
.
assertAllEqual
([[
0
], [
3
]],
x1
)
self
.
assertAllEqual
([[
1
], [
4
]],
x2
)
self
.
assertAllEqual
([[
2
], [
5
]],
x3
)
def
testExecuteBadNumOutputsArgument
(
self
):
with
self
.
assertRaises
(
TypeError
):
execute
(
b'Relu'
, [],
inputs
=
[
constant_op
.
constant
(
3.0
)],
attrs
=
(
'T'
,
dtypes
.
float32
.
as_datatype_enum
))
def
testExecuteUnknownOp
(
self
):
with
self
.
assertRaises
(
errors
.
NotFoundError
):
execute
(
b'BlahBlahBlah'
,
num_outputs
=
1
,
inputs
=
[],
attrs
=
None
)
def
testExecuteUnknownAttr
(
self
):
with
self
.
assertRaises
(
errors
.
InvalidArgumentError
):
execute
(
b'Identity'
,
num_outputs
=
1
,
inputs
=
[
constant_op
.
constant
(
3
)],
attrs
=
(
'T'
,
dtypes
.
int32
.
as_datatype_enum
,
'unknown_attr'
,
'blah'
))
def
testComposition
(
self
):
def
add
(
x
,
y
):
return
execute
(
b'Add'
,
num_outputs
=
1
,
inputs
=
[
x
,
y
],
attrs
=
(
'T'
,
dtypes
.
int32
.
as_datatype_enum
))[
0
]
x
=
constant_op
.
constant
(
1
)
three_x
=
add
(
add
(
x
,
x
),
x
)
self
.
assertEquals
(
dtypes
.
int32
,
three_x
.
dtype
)
self
.
assertAllEqual
(
3
,
three_x
)
def
testOperationWithNoInputsRunsOnDevice
(
self
):
if
not
context
.
context
().
num_gpus
():
self
.
skipTest
(
'No GPUs found'
)
shape
=
constant_op
.
constant
([],
dtype
=
dtypes
.
int32
)
# x: Run the "TruncatedNormal" op CPU and copy result to GPU.
x
=
truncated_normal
(
shape
).
gpu
()
# y: Explicitly run the "TruncatedNormal" op on GPU.
with
context
.
device
(
'gpu:0'
):
y
=
truncated_normal
(
shape
)
# Add would fail if x and y were not on the same device.
execute
(
b'Add'
,
1
,
inputs
=
[
x
,
y
],
attrs
=
(
'T'
,
x
.
dtype
.
as_datatype_enum
))
def
testInvalidDevice
(
self
):
with
self
.
assertRaises
(
ValueError
):
with
context
.
device
(
'pu:0'
):
_
=
constant_op
.
constant
(
1
)
def
testConvertMixedEagerTensors
(
self
):
array
=
np
.
zeros
((),
dtype
=
np
.
float32
)
tensor
=
constant_op
.
constant
(
0.
,
dtype
=
dtypes
.
float32
)
types
,
tensors
=
execute_lib
.
convert_to_mixed_eager_tensors
(
[
array
,
tensor
],
context
.
context
())
for
typ
,
t
in
zip
(
types
,
tensors
):
self
.
assertEquals
(
typ
,
dtypes
.
float32
)
self
.
assertIsInstance
(
t
,
ops
.
EagerTensor
)
class
SendRecvTest
(
test_util
.
TensorFlowTestCase
):
cpu_device
=
'/job:localhost/replica:0/task:0/device:CPU:0'
def
_send
(
self
,
tensor
,
tensor_name
,
to_device
):
return
execute
(
b'_Send'
,
num_outputs
=
0
,
inputs
=
[
tensor
],
attrs
=
(
'T'
,
tensor
.
dtype
.
as_datatype_enum
,
'tensor_name'
,
tensor_name
,
'send_device'
,
tensor
.
device
,
'send_device_incarnation'
,
0
,
'recv_device'
,
to_device
,
'client_terminated'
,
True
))
def
_recv
(
self
,
dtype
,
tensor_name
,
from_device
):
device_name
=
context
.
context
().
device_name
if
not
device_name
:
device_name
=
self
.
cpu_device
return
execute
(
b'_Recv'
,
num_outputs
=
1
,
inputs
=
[],
attrs
=
(
'tensor_type'
,
dtype
.
as_datatype_enum
,
'tensor_name'
,
tensor_name
,
'send_device'
,
from_device
,
'send_device_incarnation'
,
0
,
'recv_device'
,
device_name
,
'client_terminated'
,
False
))[
0
]
def
testBasic
(
self
):
t0
=
constant_op
.
constant
(
1.0
)
t1
=
constant_op
.
constant
(
2.0
)
self
.
_send
(
t0
,
't0'
,
self
.
cpu_device
)
self
.
_send
(
t1
,
't1'
,
self
.
cpu_device
)
self
.
assertAllEqual
(
self
.
_recv
(
dtypes
.
float32
,
't0'
,
self
.
cpu_device
),
1.0
)
self
.
assertAllEqual
(
self
.
_recv
(
dtypes
.
float32
,
't1'
,
self
.
cpu_device
),
2.0
)
def
testLocalCrossDevice
(
self
):
if
not
context
.
context
().
num_gpus
():
self
.
skipTest
(
'No GPUs found'
)
gpu_device_name
=
'/job:localhost/replica:0/task:0/device:GPU:0'
with
ops
.
device
(
'GPU:0'
):
t0
=
constant_op
.
constant
(
1.0
)
self
.
_send
(
t0
,
't0'
,
self
.
cpu_device
)
with
ops
.
device
(
'cpu:0'
):
self
.
assertAllEqual
(
self
.
_recv
(
dtypes
.
float32
,
't0'
,
gpu_device_name
),
1.0
)
self
.
_send
(
constant_op
.
constant
(
2.0
),
't1'
,
gpu_device_name
)
with
ops
.
device
(
'GPU:0'
):
self
.
assertAllEqual
(
self
.
_recv
(
dtypes
.
float32
,
't1'
,
self
.
cpu_device
),
2.0
)
if
__name__
==
'__main__'
:
test
.
main
()
Back
|
FazBrowse Home
|
New Git URL