提交 5b3d7c52 authored 作者: George Ho's avatar George Ho

Remove 'easy' references to theano.compat

上级 70ec78a1
...@@ -9,7 +9,7 @@ import theano ...@@ -9,7 +9,7 @@ import theano
import theano.gpuarray import theano.gpuarray
import theano.tensor as tt import theano.tensor as tt
from theano import config, gof from theano import config, gof
from theano.compat import exc_message from theano.utils import exc_message
from theano.compile import UnusedInputError, function from theano.compile import UnusedInputError, function
from theano.compile.io import In, Out from theano.compile.io import In, Out
from theano.gof import MissingInputError from theano.gof import MissingInputError
......
...@@ -8,7 +8,6 @@ import tests.unittest_tools as utt ...@@ -8,7 +8,6 @@ import tests.unittest_tools as utt
import theano import theano
from tests.gpuarray.config import mode_with_gpu from tests.gpuarray.config import mode_with_gpu
from theano import config, function, tensor from theano import config, function, tensor
from theano.compat import PY3
from theano.gpuarray.multinomial import ( from theano.gpuarray.multinomial import (
GPUAChoiceFromUniform, GPUAChoiceFromUniform,
GPUAMultinomialFromUniform, GPUAMultinomialFromUniform,
...@@ -378,9 +377,3 @@ def test_gpu_opt_wor(): ...@@ -378,9 +377,3 @@ def test_gpu_opt_wor():
def test_unpickle_legacy_op(): def test_unpickle_legacy_op():
testfile_dir = os.path.dirname(os.path.realpath(__file__)) testfile_dir = os.path.dirname(os.path.realpath(__file__))
fname = "test_gpuarray_multinomial_wo_replacement.pkl" fname = "test_gpuarray_multinomial_wo_replacement.pkl"
if not PY3:
with open(os.path.join(testfile_dir, fname)) as fp:
u = Unpickler(fp)
m = u.load()
assert isinstance(m, GPUAChoiceFromUniform)
...@@ -8,8 +8,8 @@ import logging ...@@ -8,8 +8,8 @@ import logging
import re import re
import traceback as tb import traceback as tb
import warnings import warnings
from collections import OrderedDict
from theano import compat
from theano.compile.function_module import orig_function from theano.compile.function_module import orig_function
from theano.compile.pfunc import pfunc from theano.compile.pfunc import pfunc
...@@ -291,7 +291,7 @@ def function( ...@@ -291,7 +291,7 @@ def function(
if ( if (
isinstance(updates, dict) isinstance(updates, dict)
and not isinstance(updates, compat.OrderedDict) and not isinstance(updates, OrderedDict)
and len(updates) > 1 and len(updates) > 1
): ):
warnings.warn( warnings.warn(
...@@ -300,7 +300,7 @@ def function( ...@@ -300,7 +300,7 @@ def function(
" got " + str(type(updates)) + ". Using " " got " + str(type(updates)) + ". Using "
"a standard dictionary here results in " "a standard dictionary here results in "
"non-deterministic behavior. You should use an OrderedDict" "non-deterministic behavior. You should use an OrderedDict"
" if you are using Python 2.7 (theano.compat.OrderedDict" " if you are using Python 2.7 (collections.OrderedDict"
" for older python), or use a list of (shared, update)" " for older python), or use a list of (shared, update)"
" pairs. Do not just convert your dictionary to this type before" " pairs. Do not just convert your dictionary to this type before"
" the call as the conversion will still be non-deterministic.", " the call as the conversion will still be non-deterministic.",
......
...@@ -12,7 +12,6 @@ from six.moves import StringIO ...@@ -12,7 +12,6 @@ from six.moves import StringIO
import theano import theano
from theano import config from theano import config
from theano.compat import PY3
from theano.gof import cmodule, graph, link, utils from theano.gof import cmodule, graph, link, utils
from theano.gof.callcache import CallCache from theano.gof.callcache import CallCache
from theano.gof.compilelock import get_lock, release_lock from theano.gof.compilelock import get_lock, release_lock
...@@ -1648,8 +1647,7 @@ class CLinker(link.Linker): ...@@ -1648,8 +1647,7 @@ class CLinker(link.Linker):
# Static methods that can run and destroy the struct built by # Static methods that can run and destroy the struct built by
# instantiate. # instantiate.
if PY3: static = """
static = """
static int {struct_name}_executor({struct_name} *self) {{ static int {struct_name}_executor({struct_name} *self) {{
return self->run(); return self->run();
}} }}
...@@ -1658,21 +1656,7 @@ class CLinker(link.Linker): ...@@ -1658,21 +1656,7 @@ class CLinker(link.Linker):
{struct_name} *self = ({struct_name} *)PyCapsule_GetContext(capsule); {struct_name} *self = ({struct_name} *)PyCapsule_GetContext(capsule);
delete self; delete self;
}} }}
""".format( """.format(struct_name=self.struct_name)
struct_name=self.struct_name
)
else:
static = """
static int %(struct_name)s_executor(%(struct_name)s* self) {
return self->run();
}
static void %(struct_name)s_destructor(void* executor, void* self) {
delete ((%(struct_name)s*)self);
}
""" % dict(
struct_name=self.struct_name
)
# We add all the support code, compile args, headers and libs we need. # We add all the support code, compile args, headers and libs we need.
for support_code in self.support_code() + self.c_support_code_apply: for support_code in self.support_code() + self.c_support_code_apply:
...@@ -1769,9 +1753,7 @@ class CLinker(link.Linker): ...@@ -1769,9 +1753,7 @@ class CLinker(link.Linker):
print(" delete struct_ptr;", file=code) print(" delete struct_ptr;", file=code)
print(" return NULL;", file=code) print(" return NULL;", file=code)
print(" }", file=code) print(" }", file=code)
if PY3: print("""\
print(
"""\
PyObject* thunk = PyCapsule_New((void*)(&{struct_name}_executor), NULL, {struct_name}_destructor); PyObject* thunk = PyCapsule_New((void*)(&{struct_name}_executor), NULL, {struct_name}_destructor);
if (thunk != NULL && PyCapsule_SetContext(thunk, struct_ptr) != 0) {{ if (thunk != NULL && PyCapsule_SetContext(thunk, struct_ptr) != 0) {{
PyErr_Clear(); PyErr_Clear();
...@@ -1779,16 +1761,10 @@ class CLinker(link.Linker): ...@@ -1779,16 +1761,10 @@ class CLinker(link.Linker):
thunk = NULL; thunk = NULL;
}} }}
""".format( """.format(
**locals() **locals()
), ),
file=code, file=code,
) )
else:
print(
" PyObject* thunk = PyCObject_FromVoidPtrAndDesc((void*)(&%(struct_name)s_executor), struct_ptr, %(struct_name)s_destructor);"
% locals(),
file=code,
)
print(" return thunk; }", file=code) print(" return thunk; }", file=code)
return code.getvalue() return code.getvalue()
......
...@@ -149,38 +149,28 @@ class DynamicModule: ...@@ -149,38 +149,28 @@ class DynamicModule:
print("};", file=stream) print("};", file=stream)
def print_init(self, stream): def print_init(self, stream):
if PY3: print(
print( """\
"""\
static struct PyModuleDef moduledef = {{ static struct PyModuleDef moduledef = {{
PyModuleDef_HEAD_INIT, PyModuleDef_HEAD_INIT,
"{name}", "{name}",
NULL, NULL,
-1, -1,
MyMethods, MyMethods,
}}; }};
""".format( """.format(
name=self.hash_placeholder name=self.hash_placeholder
), ),
file=stream, file=stream,
) )
print( print(
("PyMODINIT_FUNC PyInit_%s(void) {" % self.hash_placeholder), ("PyMODINIT_FUNC PyInit_%s(void) {" % self.hash_placeholder),
file=stream, file=stream,
) )
for block in self.init_blocks: for block in self.init_blocks:
print(" ", block, file=stream) print(" ", block, file=stream)
print(" PyObject *m = PyModule_Create(&moduledef);", file=stream) print(" PyObject *m = PyModule_Create(&moduledef);", file=stream)
print(" return m;", file=stream) print(" return m;", file=stream)
else:
print(("PyMODINIT_FUNC init%s(void){" % self.hash_placeholder), file=stream)
for block in self.init_blocks:
print(" ", block, file=stream)
print(
" ",
('(void) Py_InitModule("%s", MyMethods);' % self.hash_placeholder),
file=stream,
)
print("}", file=stream) print("}", file=stream)
def add_include(self, str): def add_include(self, str):
......
...@@ -3,7 +3,6 @@ import os ...@@ -3,7 +3,6 @@ import os
import sys import sys
from theano import config from theano import config
from theano.compat import PY3
from theano.gof.compilelock import get_lock, release_lock from theano.gof.compilelock import get_lock, release_lock
from . import cmodule from . import cmodule
...@@ -49,35 +48,26 @@ def compile_cutils(): ...@@ -49,35 +48,26 @@ def compile_cutils():
"Run a theano cthunk."}, "Run a theano cthunk."},
{NULL, NULL, 0, NULL} /* Sentinel */ {NULL, NULL, 0, NULL} /* Sentinel */
};""" };"""
if PY3:
# This is not the most efficient code, but it is written this way to # This is not the most efficient code, but it is written this way to
# highlight the changes needed to make 2.x code compile under python 3. # highlight the changes needed to make 2.x code compile under python 3.
code = code.replace("<Python.h>", '"numpy/npy_3kcompat.h"', 1) code = code.replace("<Python.h>", '"numpy/npy_3kcompat.h"', 1)
code = code.replace("PyCObject", "NpyCapsule") code = code.replace("PyCObject", "NpyCapsule")
code += """ code += """
static struct PyModuleDef moduledef = { static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT, PyModuleDef_HEAD_INIT,
"cutils_ext", "cutils_ext",
NULL, NULL,
-1, -1,
CutilsExtMethods, CutilsExtMethods,
}; };
PyMODINIT_FUNC PyMODINIT_FUNC
PyInit_cutils_ext(void) { PyInit_cutils_ext(void) {
return PyModule_Create(&moduledef); return PyModule_Create(&moduledef);
} }
} }
""" """
else:
code += """
PyMODINIT_FUNC
initcutils_ext(void)
{
(void) Py_InitModule("cutils_ext", CutilsExtMethods);
}
} //extern C
"""
loc = os.path.join(config.compiledir, "cutils_ext") loc = os.path.join(config.compiledir, "cutils_ext")
if not os.path.exists(loc): if not os.path.exists(loc):
......
...@@ -5,7 +5,7 @@ import sys ...@@ -5,7 +5,7 @@ import sys
from six import StringIO from six import StringIO
from theano import config from theano import config
from theano.compat import DefaultOrderedDict from theano.utils import DefaultOrderedDict
from theano.gof import opt from theano.gof import opt
from theano.misc.ordered_set import OrderedSet from theano.misc.ordered_set import OrderedSet
......
...@@ -6,7 +6,6 @@ import numpy as np ...@@ -6,7 +6,6 @@ import numpy as np
from six.moves import StringIO from six.moves import StringIO
from theano import config from theano import config
from theano.compat import PY3
def simple_extract_stack(f=None, limit=None, skips=None): def simple_extract_stack(f=None, limit=None, skips=None):
...@@ -598,28 +597,16 @@ def remove(predicate, coll): ...@@ -598,28 +597,16 @@ def remove(predicate, coll):
return [x for x in coll if not predicate(x)] return [x for x in coll if not predicate(x)]
if PY3: import hashlib
import hashlib
def hash_from_code(msg): def hash_from_code(msg):
# hashlib.sha256() requires an object that supports buffer interface, # hashlib.sha256() requires an object that supports buffer interface,
# but Python 3 (unicode) strings don't. # but Python 3 (unicode) strings don't.
if isinstance(msg, str): if isinstance(msg, str):
msg = msg.encode() msg = msg.encode()
# Python 3 does not like module names that start with # Python 3 does not like module names that start with
# a digit. # a digit.
return "m" + hashlib.sha256(msg).hexdigest() return "m" + hashlib.sha256(msg).hexdigest()
else:
import hashlib
def hash_from_code(msg):
try:
return hashlib.sha256(msg).hexdigest()
except TypeError:
assert isinstance(msg, np.ndarray)
return hashlib.sha256(np.getbuffer(msg)).hexdigest()
def hash_from_file(file_path): def hash_from_file(file_path):
......
...@@ -16,12 +16,12 @@ from copy import copy ...@@ -16,12 +16,12 @@ from copy import copy
from functools import partial from functools import partial
from itertools import chain from itertools import chain
from textwrap import dedent from textwrap import dedent
from collections.abc import Callable
import numpy as np import numpy as np
import theano import theano
from theano import config, gof, printing from theano import config, gof, printing
from theano.compat import Callable
from theano.gof import Apply, Constant, FunctionGraph, Op, Type, Variable, utils from theano.gof import Apply, Constant, FunctionGraph, Op, Type, Variable, utils
from theano.gradient import DisconnectedType, grad_undefined from theano.gradient import DisconnectedType, grad_undefined
from theano.misc.safe_asarray import _asarray from theano.misc.safe_asarray import _asarray
......
...@@ -40,7 +40,7 @@ class OrderedUpdates(OrderedDict): ...@@ -40,7 +40,7 @@ class OrderedUpdates(OrderedDict):
"non-ordered dictionary with 2+ elements could " "non-ordered dictionary with 2+ elements could "
"make your code non-deterministic. You can use " "make your code non-deterministic. You can use "
"an OrderedDict that is available at " "an OrderedDict that is available at "
"theano.compat.OrderedDict for python 2.6+." "collections.OrderedDict for python 2.6+."
) )
super().__init__(*key, **kwargs) super().__init__(*key, **kwargs)
for key in self: for key in self:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论