提交 40d0a283 authored 作者: Brandon T. Willard's avatar Brandon T. Willard 提交者: Thomas Wiecki

Use direct imports for theano.compile.io and theano.compile.mode objects

上级 54c106b7
......@@ -29,9 +29,9 @@ Conditions
z_lazy = ifelse(tt.lt(a,b), tt.mean(x), tt.mean(y))
f_switch = theano.function([a,b,x,y], z_switch,
mode=theano.Mode(linker='vm'))
mode=theano.compile.mode.Mode(linker='vm'))
f_lazyifelse = theano.function([a,b,x,y], z_lazy,
mode=theano.Mode(linker='vm'))
mode=theano.compile.mode.Mode(linker='vm'))
val1 = 0.
val2 = 1.
......
......@@ -232,11 +232,13 @@ that control how ``theano.function`` handles its argument[s] and return value[s]
.. testcode::
import theano, theano.tensor
import theano
import theano.tensor as tt
from theano.compile.io import In, Out
x = theano.tensor.matrix()
x = tt.matrix()
y = 2 * x
f = theano.function([theano.In(x, borrow=True)], theano.Out(y, borrow=True))
f = theano.function([In(x, borrow=True)], Out(y, borrow=True))
Borrowing an input means that Theano will treat the argument you provide as if
it were part of Theano's pool of temporaries. Consequently, your input
......
......@@ -31,9 +31,9 @@ IfElse vs Switch
z_lazy = ifelse(tt.lt(a, b), tt.mean(x), tt.mean(y))
f_switch = theano.function([a, b, x, y], z_switch,
mode=theano.Mode(linker='vm'))
mode=theano.compile.mode.Mode(linker='vm'))
f_lazyifelse = theano.function([a, b, x, y], z_lazy,
mode=theano.Mode(linker='vm'))
mode=theano.compile.mode.Mode(linker='vm'))
val1 = 0.
val2 = 1.
......
......@@ -210,7 +210,7 @@ The optimizers Theano provides are summarized below to indicate the trade-offs
one might make between compilation time and execution time.
These optimizers can be enabled globally with the Theano flag: ``optimizer=name``
or per call to theano functions with ``theano.function(...mode=theano.Mode(optimizer="name"))``.
or per call to theano functions with ``function(...mode=Mode(optimizer="name"))``.
================= ============ ============== ==================================================
optimizer Compile time Execution time Description
......
......@@ -11,6 +11,7 @@ import theano.tensor as tt
from theano.compile.function import function
from theano.compile.function.types import UnusedInputError
from theano.compile.io import In, Out
from theano.compile.mode import Mode
from theano.configdefaults import config
from theano.graph.basic import Constant
from theano.graph.fg import MissingInputError
......@@ -627,14 +628,12 @@ class TestFunction:
f(o + 0.1) # should not clobber the memory used to store four
assert np.all(four == 4)
f = function(
[a], Out(a * 4, borrow=True), mode=theano.Mode("c|py_nogc", "fast_run")
)
f = function([a], Out(a * 4, borrow=True), mode=Mode("c|py_nogc", "fast_run"))
o = np.ones((3, 3))
four = f(o)
assert np.all(four == 4)
f(o + 0.1) # should clobber the memory used to store four
if theano.config.cxx:
if config.cxx:
assert not np.all(four == 4)
else:
# The Elemwise.perform method don't reuse memory
......@@ -691,9 +690,7 @@ class TestFunction:
a, b = dscalars("a", "b")
c = a + b
func = theano.function(
[theano.In(a, name="first"), theano.In(b, value=1, name="second")], c
)
func = theano.function([In(a, name="first"), In(b, value=1, name="second")], c)
x = func(first=1)
try:
func(second=2)
......@@ -721,17 +718,17 @@ class TestFunction:
# Assert cases we should check for aliased inputs
for d in [
dict(
inputs=[theano.In(x1, borrow=True)],
inputs=[In(x1, borrow=True)],
outputs=[x1 + 1],
updates=[(s2, s2 + 3)],
),
dict(
inputs=[theano.In(x1, borrow=True, mutable=True)],
inputs=[In(x1, borrow=True, mutable=True)],
outputs=[x1 + 1],
updates=[(s2, s2 + 3)],
),
dict(
inputs=[theano.In(x1, mutable=True)],
inputs=[In(x1, mutable=True)],
outputs=[x1 + 1],
updates=[(s2, s2 + 3)],
),
......@@ -1146,12 +1143,12 @@ def test_empty_givens_updates():
# triggering useless crashes at compile time.
x = scalar()
y = x * 2
function([theano.In(x)], y, givens={})
function([theano.In(x)], y, updates={})
function([In(x)], y, givens={})
function([In(x)], y, updates={})
@pytest.mark.skipif(
not theano.gpuarray.pygpu_activated or theano.config.mode == "DEBUG_MODE",
not theano.gpuarray.pygpu_activated or config.mode == "DEBUG_MODE",
reason="DEBUG_MODE forces synchronous behaviour which breaks this test",
)
def test_sync_update():
......
......@@ -33,5 +33,5 @@ def test_no_output_from_implace():
def test_including():
mode = theano.Mode(optimizer="merge")
mode = Mode(optimizer="merge")
mode.including("fast_compile")
......@@ -5,7 +5,7 @@ Test compilation modes
import copy
import theano
from theano.compile import Mode
from theano.compile.mode import Mode
from theano.tensor.type import matrix, vector
......
......@@ -2,6 +2,7 @@ import numpy as np
import pytest
import theano
from theano.compile.mode import Mode
from theano.graph import fg
from theano.graph.basic import Apply, Constant, Variable
from theano.graph.op import COp
......@@ -220,7 +221,7 @@ def test_clinker_literal_inlining():
not theano.config.cxx, reason="G++ not available, so we need to skip this test."
)
def test_clinker_literal_cache():
mode = theano.Mode(linker="c")
mode = Mode(linker="c")
A = matrix()
input1 = vector()
......@@ -415,7 +416,7 @@ def test_shared_input_output():
state = theano.shared(0)
state.name = "state"
linker = CLinker()
mode = theano.Mode(linker=linker)
mode = Mode(linker=linker)
f = theano.function([inc], state, updates=[(state, state + inc)], mode=mode)
g = theano.function([inc], state, updates=[(state, state + inc)])
......
......@@ -3,8 +3,9 @@ from copy import deepcopy
import numpy as np
import theano
from theano.graph import basic, fg
from theano.graph.basic import Apply, Constant, Variable
from theano.compile.mode import Mode
from theano.graph import fg
from theano.graph.basic import Apply, Constant, Variable, clone
from theano.graph.op import Op
from theano.graph.type import Type
from theano.link.basic import Container, PerformLinker, WrapLinker
......@@ -121,7 +122,7 @@ class TestPerformLinker:
x, y, z = inputs()
a, d = add(x, y), div(x, y)
e = mul(a, d)
fn = perform_linker(FunctionGraph(*basic.clone([x, y, a], [e]))).make_function()
fn = perform_linker(FunctionGraph(*clone([x, y, a], [e]))).make_function()
assert fn(1.0, 2.0, 9.0) == 4.5
def test_skiphole(self):
......@@ -129,7 +130,7 @@ class TestPerformLinker:
a = add(x, y)
r = raise_err(a)
e = add(r, a)
fn = perform_linker(FunctionGraph(*basic.clone([x, y, r], [e]))).make_function()
fn = perform_linker(FunctionGraph(*clone([x, y, r], [e]))).make_function()
assert fn(1.0, 2.0, 4.5) == 7.5
......@@ -186,7 +187,7 @@ def test_sort_schedule_fn():
return cmp(str(a), str(b)) # lexicographical sort
linker = OpWiseCLinker(schedule=sort_schedule_fn(str_cmp))
mode = theano.Mode(linker=linker)
mode = Mode(linker=linker)
f = theano.function((x,), (y,), mode=mode)
nodes = f.maker.linker.make_all()[-1]
......
......@@ -8,7 +8,8 @@ import pytest
import theano
from theano import function
from theano import tensor as tt
from theano.compile import Mode
from theano.compile.io import In
from theano.compile.mode import Mode
from theano.configdefaults import config
from theano.graph.basic import Apply
from theano.graph.op import Op
......@@ -389,8 +390,8 @@ def test_vm_gc():
x = vector()
p = RunOnce()(x)
mode = theano.Mode(linker=VMLinker(lazy=True))
f = theano.function([theano.In(x, mutable=True)], [p + 1, p + 2], mode=mode)
mode = Mode(linker=VMLinker(lazy=True))
f = theano.function([In(x, mutable=True)], [p + 1, p + 2], mode=mode)
f([1, 2, 3])
p = RunOnce()(x)
......@@ -408,7 +409,7 @@ def test_reallocation():
VMLinker(allow_gc=False, lazy=False, use_cloop=False),
VMLinker(allow_gc=True, lazy=False, use_cloop=False),
]:
m = theano.compile.get_mode(theano.Mode(linker=linker))
m = theano.compile.get_mode(Mode(linker=linker))
m = m.excluding("fusion", "inplace")
f = theano.function([x, y], z, name="test_reduce_memory", mode=m)
......@@ -444,7 +445,7 @@ def test_no_recycling():
VMLinker(use_cloop=False, lazy=False, allow_gc=False),
]:
mode = theano.Mode(optimizer="fast_compile", linker=lnk)
mode = Mode(optimizer="fast_compile", linker=lnk)
f = theano.function([x], x + 1, mode=mode)
f2 = theano.function([x], (x + 1) * 2, mode=mode)
m1 = f.fn.thunks[0].thunk.module
......
from theano.compile import Mode
from theano.compile.mode import Mode
from theano.configdefaults import config
from theano.link.basic import WrapLinkerMany
from theano.link.vm import VMLinker
......
......@@ -13,6 +13,7 @@ import pytest
import tests.unittest_tools as utt
import theano
from theano.compile.mode import Mode
from theano.graph.fg import FunctionGraph
from theano.link.c.basic import DualLinker
from theano.scalar.basic import (
......@@ -453,7 +454,7 @@ def test_grad_abs():
a = fscalar("a")
b = theano.tensor.nnet.relu(a)
c = theano.grad(b, a)
f = theano.function([a], c, mode=theano.Mode(optimizer=None))
f = theano.function([a], c, mode=Mode(optimizer=None))
# Currently Theano return 0.5, but it isn't sure it won't change
# in the futur.
ret = f(0.0)
......
......@@ -26,6 +26,8 @@ import theano.scalar.sharedvar
from tests import unittest_tools as utt
from theano import tensor as tt
from theano.compile.function.pfunc import rebuild_collect_shared
from theano.compile.io import In
from theano.compile.mode import FAST_RUN, Mode, get_default_mode, get_mode
from theano.configdefaults import config
from theano.misc.safe_asarray import _asarray
from theano.scan.basic import scan
......@@ -54,11 +56,11 @@ from theano.tensor.type import (
if config.mode == "FAST_COMPILE":
mode_with_opt = theano.compile.mode.get_mode("FAST_RUN")
mode_with_opt = get_mode("FAST_RUN")
else:
mode_with_opt = theano.compile.mode.get_default_mode()
mode_with_opt = get_default_mode()
if config.mode in ("DEBUG_MODE", "DebugMode"):
mode_nodebug = theano.compile.mode.get_mode("FAST_RUN")
mode_nodebug = get_mode("FAST_RUN")
else:
mode_nodebug = mode_with_opt
......@@ -234,9 +236,7 @@ class TestScan:
# generator network, only one output , type scalar ; no sequence or
# non sequence arguments
@pytest.mark.skipif(
isinstance(
theano.compile.mode.get_default_mode(), theano.compile.debugmode.DebugMode
),
isinstance(get_default_mode(), theano.compile.debugmode.DebugMode),
reason="This test fails in DebugMode, because it is not yet picklable.",
)
def test_pickling(self):
......@@ -920,14 +920,14 @@ class TestScan:
u0 = vector("u0")
u1 = vector("u1")
u2 = vector("u2")
mu0 = theano.In(u0, mutable=False)
mu1 = theano.In(u1, mutable=True)
mu2 = theano.In(u2, mutable=True)
mu0 = In(u0, mutable=False)
mu1 = In(u1, mutable=True)
mu2 = In(u2, mutable=True)
x0 = scalar("x0")
x1 = scalar("y0")
W_in = theano.shared(vW_in, "Win")
W = theano.shared(vW, "W")
mode = theano.compile.mode.get_mode(None).including("inplace")
mode = get_mode(None).including("inplace")
def f_rnn_shared(u0_t, u1_t, u2_t, x0_tm1, x1_tm1):
return [
......@@ -987,14 +987,14 @@ class TestScan:
u0 = vector("u0")
u1 = vector("u1")
u2 = vector("u2")
mu0 = theano.In(u0, mutable=True)
mu1 = theano.In(u1, mutable=True)
mu2 = theano.In(u2, mutable=True)
mu0 = In(u0, mutable=True)
mu1 = In(u1, mutable=True)
mu2 = In(u2, mutable=True)
x0 = scalar("x0")
x1 = scalar("y0")
W_in = theano.shared(vW_in, "Win")
W = theano.shared(vW, "W")
mode = theano.compile.mode.get_mode(None).including("inplace")
mode = get_mode(None).including("inplace")
def f_rnn_shared(u0_t, u1_t, u1_tp1, u2_tm1, u2_t, u2_tp1, x0_tm1, x1_tm1):
return [
......@@ -1057,7 +1057,7 @@ class TestScan:
x0 = tt.constant(x0)
to_replace = outputs[0].owner.inputs[0].owner.inputs[1]
outputs = theano.clone(outputs, replace=[(to_replace, x0)])
mode = theano.compile.mode.get_mode(None).including("inplace")
mode = get_mode(None).including("inplace")
f9 = theano.function([], outputs, updates=updates, mode=mode)
scan_node = [x for x in f9.maker.fgraph.toposort() if isinstance(x.op, Scan)]
assert 0 not in scan_node[0].op.destroy_map.keys()
......@@ -2783,7 +2783,7 @@ class TestScan:
x,
tt.constant(np.asarray(0.0, dtype=config.floatX)),
)
mode = theano.compile.mode.FAST_RUN
mode = FAST_RUN
mode = mode.excluding("inplace")
f1 = theano.function([], o, mode=mode)
inputs, outputs = clone_optimized_graph(f1)
......@@ -2817,7 +2817,7 @@ class TestScan:
tt.constant(np.asarray(0.0, dtype=config.floatX)),
)
mode = theano.compile.mode.FAST_RUN
mode = FAST_RUN
mode = mode.excluding("inplace")
f0 = theano.function([], o, mode=mode)
inputs, outputs = clone_optimized_graph(f0)
......@@ -2852,7 +2852,7 @@ class TestScan:
tt.constant(np.asarray(0.0, dtype=config.floatX)),
)
mode = theano.compile.mode.FAST_RUN
mode = FAST_RUN
mode = mode.excluding("inplace")
f1 = theano.function([], o, mode=mode)
inputs, outputs = clone_optimized_graph(f1)
......@@ -4186,7 +4186,7 @@ class TestScan:
[U, x1, x2],
[X1, X2, X3],
updates=updates,
mode=theano.Mode(linker="py"),
mode=Mode(linker="py"),
allow_input_downcast=True,
)
rng = np.random.RandomState(utt.fetch_seed())
......@@ -4223,7 +4223,7 @@ class TestScan:
[W, x1, x2],
[X1, X2, X3],
updates=updates,
mode=theano.Mode(linker="py"),
mode=Mode(linker="py"),
allow_input_downcast=True,
)
rng = np.random.RandomState(utt.fetch_seed())
......@@ -4594,7 +4594,7 @@ def test_speed():
# fn=lambda ri, rii: ri + rii,
# sequences=[s_r[1:]],
# outputs_info=tt.constant(r[0]),
# mode=theano.Mode(linker="cvm"),
# mode=Mode(linker="cvm"),
# )
# assert not updates
#
......@@ -4616,7 +4616,7 @@ def test_speed():
[],
[],
updates=OrderedDict([(s_i, s_i + 1), (shared_r, s_rinc)]),
mode=theano.Mode(linker="cvm"),
mode=Mode(linker="cvm"),
)
f_cvm_shared._check_for_aliased_inputs = False
......@@ -4652,11 +4652,11 @@ def test_speed_rnn():
# fn=lambda ri, rii: tt.tanh(tt.dot(rii, w)),
# sequences=[s_r[1:]],
# outputs_info=tt.constant(r[0]),
# mode=theano.Mode(linker="cvm"),
# mode=Mode(linker="cvm"),
# )
# assert not updates
#
# f_cvm = theano.function([s_r], s_y, mode=theano.Mode(linker="cvm"))
# f_cvm = theano.function([s_r], s_y, mode=Mode(linker="cvm"))
#
# cvm_duration = timeit(lambda: f_cvm(r), number=n_timeit)
......@@ -4675,7 +4675,7 @@ def test_speed_rnn():
[],
[],
updates=OrderedDict([(s_i, s_i + 1), (shared_r, s_rinc)]),
mode=theano.Mode(linker="cvm"),
mode=Mode(linker="cvm"),
)
cvm_shared_duration = timeit(lambda: f_cvm_shared(), number=n_timeit)
......@@ -4691,7 +4691,7 @@ def test_speed_batchrnn():
This function prints out the speed of recurrent neural network
calculations implemented in various ways.
We force the mode to theano.Mode(linker='cvm'). If you manually
We force the mode to Mode(linker='cvm'). If you manually
change this code to use DebugMode this will test the correctness
of the optimizations applied, but generally correctness-testing
is not the goal of this test.
......@@ -4725,7 +4725,7 @@ def test_speed_batchrnn():
[],
[],
updates=[(s_i, s_i + 1), (shared_r, s_rinc)],
mode=theano.Mode(linker="cvm"),
mode=Mode(linker="cvm"),
)
f_fn = f.fn
t2 = time.time()
......
......@@ -11,6 +11,7 @@ from tests import unittest_tools as utt
from tests.tensor.test_sharedvar import makeSharedTester
from theano import sparse
from theano.compile.function import function
from theano.compile.io import In, Out
from theano.configdefaults import config
from theano.gradient import GradientError
from theano.graph.basic import Apply, Constant
......@@ -1357,7 +1358,7 @@ class TestStructuredDot:
a = SparseType(sparse_format_a, dtype=sparse_dtype)()
b = SparseType(sparse_format_b, dtype=sparse_dtype)()
d = tt.dot(a, b)
f = theano.function([a, b], theano.Out(d, borrow=True))
f = theano.function([a, b], Out(d, borrow=True))
for M, N, K, nnz in [
(4, 3, 2, 3),
(40, 30, 20, 3),
......@@ -1379,7 +1380,7 @@ class TestStructuredDot:
a = SparseType("csc", dtype=sparse_dtype)()
b = matrix(dtype=dense_dtype)
d = tt.dot(a, b)
f = theano.function([a, b], theano.Out(d, borrow=True))
f = theano.function([a, b], Out(d, borrow=True))
for M, N, K, nnz in [
(4, 3, 2, 3),
......@@ -1928,9 +1929,7 @@ def test_sparse_shared_memory():
sdot = sparse.structured_dot
z = sdot(x * 3, m1) + sdot(y * 2, m2)
f = theano.function(
[theano.In(x, mutable=True), theano.In(y, mutable=True)], z, mode="FAST_RUN"
)
f = theano.function([In(x, mutable=True), In(y, mutable=True)], z, mode="FAST_RUN")
def f_(x, y, m1=m1, m2=m2):
return ((x * 3) * m1) + ((y * 2) * m2)
......@@ -2243,9 +2242,7 @@ class TestRemove0(utt.InferShapeTester):
# the In thingy has to be there because theano has as rule not
# to optimize inputs
f = theano.function(
[theano.In(x, borrow=True, mutable=True)], Remove0()(x)
)
f = theano.function([In(x, borrow=True, mutable=True)], Remove0()(x))
# assert optimization local_inplace_remove0 is applied in
# modes with optimization
......
......@@ -10,6 +10,7 @@ import theano.tensor as tt
from tests import unittest_tools as utt
from tests.sparse.test_basic import random_lil
from theano import sparse
from theano.compile.mode import Mode, get_default_mode
from theano.configdefaults import config
from theano.tensor.type import ivector, matrix, vector
......@@ -17,7 +18,7 @@ from theano.tensor.type import ivector, matrix, vector
def test_local_csm_properties_csm():
data = vector()
indices, indptr, shape = (ivector(), ivector(), ivector())
mode = theano.compile.mode.get_default_mode()
mode = get_default_mode()
mode = mode.including("specialize", "local_csm_properties_csm")
for CS, cast in [
(sparse.CSC, sp.sparse.csc_matrix),
......@@ -43,10 +44,10 @@ def test_local_csm_properties_csm():
def test_local_csm_grad_c():
data = vector()
indices, indptr, shape = (ivector(), ivector(), ivector())
mode = theano.compile.mode.get_default_mode()
mode = get_default_mode()
if theano.config.mode == "FAST_COMPILE":
mode = theano.compile.Mode(linker="c|py", optimizer="fast_compile")
mode = Mode(linker="c|py", optimizer="fast_compile")
mode = mode.including("specialize", "local_csm_grad_c")
for CS, cast in [
......@@ -68,7 +69,7 @@ def test_local_csm_grad_c():
not theano.config.cxx, reason="G++ not available, so we need to skip this test."
)
def test_local_mul_s_d():
mode = theano.compile.mode.get_default_mode()
mode = get_default_mode()
mode = mode.including("specialize", "local_mul_s_d")
for sp_format in sparse.sparse_formats:
......@@ -85,7 +86,7 @@ def test_local_mul_s_d():
not theano.config.cxx, reason="G++ not available, so we need to skip this test."
)
def test_local_mul_s_v():
mode = theano.compile.mode.get_default_mode()
mode = get_default_mode()
mode = mode.including("specialize", "local_mul_s_v")
for sp_format in ["csr"]: # Not implemented for other format
......@@ -102,7 +103,7 @@ def test_local_mul_s_v():
not theano.config.cxx, reason="G++ not available, so we need to skip this test."
)
def test_local_structured_add_s_v():
mode = theano.compile.mode.get_default_mode()
mode = get_default_mode()
mode = mode.including("specialize", "local_structured_add_s_v")
for sp_format in ["csr"]: # Not implemented for other format
......@@ -120,7 +121,7 @@ def test_local_structured_add_s_v():
not theano.config.cxx, reason="G++ not available, so we need to skip this test."
)
def test_local_sampling_dot_csr():
mode = theano.compile.mode.get_default_mode()
mode = get_default_mode()
mode = mode.including("specialize", "local_sampling_dot_csr")
for sp_format in ["csr"]: # Not implemented for other format
......@@ -147,7 +148,7 @@ def test_local_sampling_dot_csr():
def test_local_dense_from_sparse_sparse_from_dense():
mode = theano.compile.mode.get_default_mode()
mode = get_default_mode()
mode = mode.including("local_dense_from_sparse_sparse_from_dense")
m = matrix()
......
......@@ -4,6 +4,7 @@ import pytest
import theano
import theano.tensor as tt
from tests import unittest_tools as utt
from theano.compile.mode import Mode
from theano.configdefaults import config
from theano.graph.opt import check_stack_trace
from theano.tensor.nnet import abstract_conv as conv
......@@ -1047,7 +1048,7 @@ class TestAbstractConvNoOptim(BaseTestConv2d):
def run_test_case(self, i, f, s, b, flip, provide_shape, fd=(1, 1)):
o = self.get_output_shape(i, f, s, b, fd)
mode = theano.Mode(optimizer=None)
mode = Mode(optimizer=None)
self.run_fwd(
inputs_shape=i,
filters_shape=f,
......@@ -1093,7 +1094,7 @@ class TestAbstractConvNoOptim(BaseTestConv2d):
def run_test_case_gi(
self, i, f, o, s, b, flip, provide_shape, fd=(1, 1), expect_error=False
):
mode = theano.Mode(optimizer=None)
mode = Mode(optimizer=None)
if not expect_error:
self.run_gradinput(
inputs_shape=i,
......@@ -2059,7 +2060,7 @@ class TestGroupedConvNoOptim:
conv_op = theano.tensor.nnet.abstract_conv.AbstractConv2d
conv_gradw_op = theano.tensor.nnet.abstract_conv.AbstractConv2d_gradWeights
conv_gradi_op = theano.tensor.nnet.abstract_conv.AbstractConv2d_gradInputs
mode = theano.Mode(optimizer=None)
mode = Mode(optimizer=None)
is_dnn = False
def setup_method(self):
......@@ -2269,7 +2270,7 @@ class TestGroupedConv3dNoOptim(TestGroupedConvNoOptim):
conv_op = theano.tensor.nnet.abstract_conv.AbstractConv3d
conv_gradw_op = theano.tensor.nnet.abstract_conv.AbstractConv3d_gradWeights
conv_gradi_op = theano.tensor.nnet.abstract_conv.AbstractConv3d_gradInputs
mode = theano.Mode(optimizer=None)
mode = Mode(optimizer=None)
def setup_method(self):
self.num_groups = [3, 2, 4, 4]
......@@ -2509,7 +2510,7 @@ class TestUnsharedConv:
conv2d_gradw_op = theano.tensor.nnet.abstract_conv.AbstractConv2d_gradWeights
conv2d_gradi_op = theano.tensor.nnet.abstract_conv.AbstractConv2d_gradInputs
mode = theano.compile.mode.Mode(optimizer="None")
mode = Mode(optimizer="None")
def setup_method(self):
self.img_shape = [(2, 2, 4, 4), (3, 2, 4, 2), (3, 3, 5, 3), (3, 4, 4, 4)]
......@@ -2741,7 +2742,7 @@ class TestAsymmetricPadding:
conv2d_gradw_op = theano.tensor.nnet.abstract_conv.AbstractConv2d_gradWeights
conv2d_gradi_op = theano.tensor.nnet.abstract_conv.AbstractConv2d_gradInputs
mode = theano.compile.mode.Mode(optimizer="None")
mode = Mode(optimizer="None")
img_shape = [(2, 2, 4, 4), (3, 2, 4, 2), (3, 3, 5, 3)]
kern_shape = [(4, 2, 2, 2), (2, 2, 4, 2), (2, 3, 3, 3)]
......@@ -2911,7 +2912,7 @@ class TestAsymmetricPadding:
class TestCausalConv:
mode = theano.compile.mode.Mode(optimizer="None")
mode = Mode(optimizer="None")
img = np.array(
[
......
......@@ -6,6 +6,7 @@ import pytest
import theano
import theano.tensor as tt
from tests import unittest_tools as utt
from theano.compile.mode import Mode
from theano.tensor.basic import NotScalarConstantError, _allclose
from theano.tensor.nnet import conv, conv2d
from theano.tensor.type import dmatrix, dtensor3, dtensor4, dvector, scalar, tensor4
......@@ -607,7 +608,7 @@ class TestConv2D(utt.InferShapeTester):
unroll_patch=True,
openmp=openmp,
)
mode = theano.Mode(
mode = Mode(
linker=theano.link.vm.VMLinker(
allow_gc=False, use_cloop=True
)
......
......@@ -13,7 +13,6 @@ from numpy.testing import assert_allclose, assert_almost_equal, assert_array_equ
import theano
import theano.scalar as ts
import theano.tensor as tt
import theano.tensor.var as var
from tests import unittest_tools as utt
from tests.tensor.utils import (
ALL_DTYPES,
......@@ -73,6 +72,7 @@ from tests.tensor.utils import (
from theano import compile, config, function, shared
from theano.assert_op import Assert
from theano.compile import DeepCopyOp
from theano.compile.io import In, Out
from theano.compile.mode import get_default_mode
from theano.gradient import grad, hessian, numeric_grad
from theano.graph.basic import Apply, Variable
......@@ -170,6 +170,7 @@ from theano.tensor import (
triu,
true_div,
unbroadcast,
var,
vertical_stack,
)
from theano.tensor.elemwise import DimShuffle, Elemwise
......@@ -3224,7 +3225,7 @@ def test_join_inplace():
join = Join(view=0)
c = join(0, x, z, z)
f = theano.function([theano.In(x, borrow=True), s], theano.Out(c, borrow=True))
f = theano.function([In(x, borrow=True), s], Out(c, borrow=True))
data = np.array([3, 4, 5], dtype=config.floatX)
print(f(data, 0))
......
......@@ -25,6 +25,7 @@ from tests import unittest_tools
from tests.tensor.utils import inplace_func
from theano import shared
from theano.compile.io import In
from theano.compile.mode import Mode
from theano.configdefaults import config
from theano.graph.fg import FunctionGraph
from theano.misc.safe_asarray import _asarray
......@@ -139,7 +140,7 @@ class TestGemm:
f = inplace_func(
[tz, ta, tx, ty, tb],
gemm_inplace(tz, ta, tx, ty, tb),
mode=theano.compile.Mode(optimizer=None, linker=l),
mode=Mode(optimizer=None, linker=l),
)
f(z, a, x, y, b)
z_after = self._gemm(z_orig, a, x, y, b)
......@@ -304,12 +305,12 @@ class TestGemm:
tz, ta, tx, ty, tb = [shared(p) for p in (z, a, x, y, b)]
# f = inplace_func([tz,ta,tx,ty,tb], gemm_inplace(tz,ta,tx,ty,tb),
# mode = theano.compile.Mode(optimizer = None, linker=l))
# mode = Mode(optimizer = None, linker=l))
# f(z, a, x, y, b)
f = inplace_func(
[],
gemm_inplace(tz, ta, tx, ty, tb),
mode=theano.compile.Mode(optimizer=None, linker=l),
mode=Mode(optimizer=None, linker=l),
)
f()
unittest_tools.assert_allclose(z_after, tz.get_value(borrow=True))
......@@ -366,7 +367,7 @@ class TestGemm:
f_i = inplace_func(
[],
gemm_inplace(tz[:, :, i], ta, tx[:, :, i], ty[:, :, i], tb),
mode=theano.compile.Mode(optimizer=None, linker=l),
mode=Mode(optimizer=None, linker=l),
)
for j in range(3):
# tz will not _always_ be overwritten,
......@@ -385,7 +386,7 @@ class TestGemm:
[],
tz_i,
updates=[(tz, tt.set_subtensor(tz[:, :, i], tz_i))],
mode=theano.compile.Mode(optimizer=None, linker=l),
mode=Mode(optimizer=None, linker=l),
)
for j in range(3):
g_i()
......@@ -645,7 +646,7 @@ def just_gemm(i, o, ishapes=None, max_graphlen=0, expected_nb_gemm=1):
g = inplace_func(
i,
o,
mode=theano.compile.Mode(linker="py", optimizer=None),
mode=Mode(linker="py", optimizer=None),
allow_input_downcast=True,
on_unused_input="ignore",
)
......@@ -734,7 +735,7 @@ def test_gemm_opt_double_gemm():
g = inplace_func(
i,
o,
mode=theano.compile.Mode(linker="py", optimizer=None),
mode=Mode(linker="py", optimizer=None),
on_unused_input="ignore",
)
......
......@@ -3,7 +3,7 @@ import numpy as np
import theano
import theano.tensor.basic as basic
from theano import function
from theano.compile import In
from theano.compile.io import In
from theano.misc.safe_asarray import _asarray
from theano.tensor.basic import (
_convert_to_float32,
......
......@@ -1284,9 +1284,7 @@ class TestElemwise(unittest_tools.InferShapeTester):
# it overflowed in this case.
a, b, c, d, e, f = vectors("abcdef")
s = a + b + c + d + e + f
g = theano.function(
[a, b, c, d, e, f], s, mode=theano.compile.Mode(linker="py")
)
g = theano.function([a, b, c, d, e, f], s, mode=Mode(linker="py"))
g(*[np.zeros(2 ** 11, config.floatX) for i in range(6)])
......
......@@ -4,6 +4,7 @@ import time
import numpy as np
import theano
from theano.compile.mode import Mode
from theano.link.basic import PerformLinker
from theano.link.c.basic import OpWiseCLinker
from theano.tensor.type import dvector, lvector
......@@ -42,12 +43,8 @@ def test_gc_never_pickles_temporaries():
# g_linker has no garbage collection
f = theano.function(
[x], r, mode=theano.Mode(optimizer=optimizer, linker=f_linker)
)
g = theano.function(
[x], r, mode=theano.Mode(optimizer=optimizer, linker=g_linker)
)
f = theano.function([x], r, mode=Mode(optimizer=optimizer, linker=f_linker))
g = theano.function([x], r, mode=Mode(optimizer=optimizer, linker=g_linker))
pre_f = pickle.dumps(f)
# pre_g = pickle.dumps(g)
......
......@@ -4,6 +4,7 @@ import pytest
import theano
import theano.tensor as tt
from theano import function
from theano.compile.mode import Mode
from theano.tensor.elemwise import DimShuffle
from theano.tensor.type import dtensor3
......@@ -43,7 +44,7 @@ class TestKeepDims:
a = np.random.rand(3, 2, 4)
# We don't need to test all opt and C code, as this is tested
# by the ops tests.
mode = theano.compile.Mode(optimizer="fast_compile", linker="py")
mode = Mode(optimizer="fast_compile", linker="py")
# 'max_and_argmax' has two outputs and can be specified with either
# a single or every axis:
......@@ -177,7 +178,7 @@ class TestKeepDims:
x = dtensor3()
a = np.random.rand(3, 2, 4).astype(theano.config.floatX)
mode = theano.compile.Mode(optimizer="fast_compile", linker="py")
mode = Mode(optimizer="fast_compile", linker="py")
for axis in [
0,
......
......@@ -4,6 +4,7 @@ import subprocess
import pytest
import theano
from theano.compile.mode import Mode
from theano.configdefaults import config
from theano.graph.sched import sort_schedule_fn
from theano.link.c.basic import OpWiseCLinker
......@@ -21,7 +22,7 @@ from theano.tensor.type import matrix
mpi_scheduler = sort_schedule_fn(*mpi_cmps)
mpi_linker = OpWiseCLinker(schedule=mpi_scheduler)
mpi_mode = theano.Mode(linker=mpi_linker)
mpi_mode = Mode(linker=mpi_linker)
@config.change_flags(compute_test_value="off")
......
差异被折叠。
......@@ -6,6 +6,7 @@ import pytest
import theano
from tests import unittest_tools as utt
from theano.compile.mode import Mode
from theano.tensor.sort import (
ArgSortOp,
SortOp,
......@@ -428,7 +429,7 @@ class TestTopK:
# So don't use DebugMode here.
mode = self.mode
if isinstance(self.mode, theano.compile.debugmode.DebugMode):
mode = theano.Mode(optimizer=mode.optimizer)
mode = Mode(optimizer=mode.optimizer)
fn = theano.function([x], y, mode=mode)
assert any(
[isinstance(n.op, self.op_class) for n in fn.maker.fgraph.apply_nodes]
......
......@@ -12,6 +12,7 @@ import theano.tensor as tt
from tests import unittest_tools as utt
from tests.tensor.utils import inplace_func, rand, randint_ranged
from theano.compile import DeepCopyOp, shared
from theano.compile.io import In
from theano.configdefaults import config
from theano.graph.op import get_test_value
from theano.graph.toolbox import is_same_graph
......@@ -1265,7 +1266,7 @@ class TestSubtensor(utt.OptimizationTestMixin):
data_copy[idx] = inc_num
else:
data_copy[idx] += inc_num
data_var = theano.In(data_var, mutable=True)
data_var = In(data_var, mutable=True)
# Remember data for the Theano function (see below).
all_inputs_var += [data_var, idx_var, inc_var]
......
......@@ -249,7 +249,7 @@ def test_mode_apply():
with pytest.raises(ValueError, match="Expected one of"):
configdefaults._filter_mode("not_a_mode")
# test with theano.Mode instance
# test with Mode instance
import theano.compile.mode
assert (
......
......@@ -282,8 +282,7 @@ class Param(In):
borrow=None,
):
warnings.warn(
"The Param class is deprecated. Replace Param(default=N)"
" by theano.In(value=N)",
"The Param class is deprecated. Replace Param(default=N)" " by In(value=N)",
stacklevel=2,
)
super().__init__(
......@@ -322,7 +321,7 @@ def pfunc(
Function parameters, these are not allowed to be shared variables.
outputs : list of Variables or Out instances
Expressions to compute.
mode : string or `theano.compile.Mode` instance
mode : string or `theano.compile.mode.Mode` instance
Compilation mode.
updates : iterable over pairs (shared_variable, new_expression). List, tuple or dict.
Update the values for SharedVariable inputs according to these
......
......@@ -111,14 +111,14 @@ def _filter_mode(val):
if val in str_options:
return val
# This can be executed before Theano is completly imported, so
# theano.Mode is not always available.
# Instead of isinstance(val, theano.Mode),
# theano.compile.mode.Mode is not always available.
# Instead of isinstance(val, theano.compile.mode.Mode),
# we can inspect the __mro__ of the object!
for type_ in type(val).__mro__:
if "theano.compile.mode.Mode" in str(type_):
return val
raise ValueError(
f"Expected one of {str_options}, or an instance of theano.Mode. "
f"Expected one of {str_options}, or an instance of theano.compile.mode.Mode. "
f"Instead got: {val}."
)
......
......@@ -10,6 +10,8 @@ import theano
import theano.pathparse
import theano.tensor as tt
from theano.assert_op import Assert
from theano.compile.io import Out
from theano.compile.mode import Mode
from theano.compile.ops import shape_i, shape_i_op
from theano.configdefaults import SUPPORTED_DNN_CONV_ALGO_RUNTIME, config
from theano.gpuarray import cudnn_defs, pygpu
......@@ -285,7 +287,7 @@ class MakerCDataType(CDataType):
self._fn = theano.function(
[v],
CDataMaker(self)(v),
mode=theano.Mode(optimizer=None),
mode=Mode(optimizer=None),
profile=False,
)
return self._fn
......@@ -421,9 +423,7 @@ def version(raises=True):
return -1
if version.v is None:
f = theano.function(
[], DnnVersion()(), theano.Mode(optimizer=None), profile=False
)
f = theano.function([], DnnVersion()(), Mode(optimizer=None), profile=False)
v = f()
if v[0] != v[1]:
raise RuntimeError(
......@@ -2632,7 +2632,7 @@ def _make_dropout_desc(dropout, seed, context_name):
desc, states = theano.function(
[],
_DropoutDescriptor(context_name)(dropout, seed, context_name),
theano.Mode(optimizer=None),
Mode(optimizer=None),
profile=False,
)()
return desc, states
......@@ -2740,7 +2740,7 @@ def _make_rnn_desc(
_RNNDescriptor(context_name)(
hidden_size, num_layers, ddesc, input_mode, direction_mode, rnn_mode, dtype
),
theano.Mode(optimizer=None),
Mode(optimizer=None),
profile=False,
)()
return desc
......@@ -2774,7 +2774,7 @@ def _get_param_size(desc, input_size, dtype, context_name):
return theano.function(
[],
_RNNParamSize(context_name)(desc, input_size, typecode),
theano.Mode(optimizer=None),
Mode(optimizer=None),
profile=False,
)()
......@@ -3016,8 +3016,8 @@ class _RNNSplitParams(DnnBase):
def _split_rnn_params(w, desc, layer, input_size, dtype, rnn_mode):
typecode = gpuarray.dtype_to_typecode(dtype)
outs = _RNNSplitParams(rnn_mode)(w, desc, layer, input_size, typecode)
outs = [theano.Out(o, borrow=True) for o in outs]
return theano.function([], outs, theano.Mode(optimizer=None), profile=False)()
outs = [Out(o, borrow=True) for o in outs]
return theano.function([], outs, Mode(optimizer=None), profile=False)()
class GpuDnnRNNOp(DnnBase):
......
......@@ -14,8 +14,8 @@ from io import StringIO
import numpy as np
import theano
from theano.compile import Function, SharedVariable, debugmode
from theano.compile.io import In, Out
from theano.configdefaults import config
from theano.graph.basic import (
Apply,
......@@ -176,7 +176,7 @@ def debugprint(
order.extend([topo for item in obj.outputs])
elif isinstance(obj, (int, float, np.ndarray)):
print(obj, file=_file)
elif isinstance(obj, (theano.In, theano.Out)):
elif isinstance(obj, (In, Out)):
results_to_print.append(obj.variable)
profile_list.append(None)
smap.append(None)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论