Page 1 of 2

SyntaxError: invalid micropython decorator

Posted: Tue Oct 11, 2016 8:37 pm
by danielm
When trying @micropython.native or @micropython.viper decorator on MicroPython v1.8.4-54-gec078af-dirty on 2016-09-22; WiPy with CC3200.

Do I have to enable it manually somewhere before building MP?

Re: SyntaxError: invalid micropython decorator

Posted: Tue Oct 11, 2016 8:45 pm
by dhylands
@micropython.native and @micropython.viper both require MICROPY_EMIT_NATIVE.
https://github.com/micropython/micropyt ... #L757-L762

It looks like EMIT_NATIVE is defined here:
https://github.com/micropython/micropyt ... fig.h#L291

and the wipy (cc3200) has those disabled:
https://github.com/micropython/micropyt ... .h#L41-L42

Re: SyntaxError: invalid micropython decorator

Posted: Tue Oct 11, 2016 9:01 pm
by danielm
Thanks Dave, I will rebuild tomorrow and check if it works.

Could you please suggest some typical test function on which I should observe improvement when defined with @micropython.native or @micropython.viper decorators?

Re: SyntaxError: invalid micropython decorator

Posted: Tue Oct 11, 2016 9:11 pm
by dhylands
I've not really used either, but there is some discussion about it here:
http://docs.micropython.org/en/latest/p ... de-emitter
with at least a viper example.

Re: SyntaxError: invalid micropython decorator

Posted: Wed Oct 12, 2016 6:20 pm
by danielm
I was not able to build MP with MICROPY_EMIT_THUMB and MICROPY_EMIT_INLINE_THUMB set to (1)

Code: Select all

CC ../py/compile.c
../py/compile.c: In function 'compile_built_in_decorator':
../py/compile.c:759:24: error: 'MP_QSTR_native' undeclared (first use in this function)
     } else if (attr == MP_QSTR_native) {
                        ^
../py/compile.c:759:24: note: each undeclared identifier is reported only once for each function it appears in
../py/compile.c:761:24: error: 'MP_QSTR_viper' undeclared (first use in this function)
     } else if (attr == MP_QSTR_viper) {
                        ^
../py/compile.c:765:24: error: 'MP_QSTR_asm_thumb' undeclared (first use in this function)
     } else if (attr == MP_QSTR_asm_thumb) {
                        ^
../py/compile.c: In function 'compile_scope_inline_asm':
../py/compile.c:3147:22: error: 'MP_QSTR_uint' undeclared (first use in this function)
                 case MP_QSTR_uint: type_sig = MP_NATIVE_TYPE_UINT; break;
                      ^
../py/compile.c:3196:19: error: 'MP_QSTR_label' undeclared (first use in this function)
         if (op == MP_QSTR_label) {
                   ^
../py/compile.c:3208:26: error: 'MP_QSTR_align' undeclared (first use in this function)
         } else if (op == MP_QSTR_align) {
                          ^
../py/compile.c:3216:26: error: 'MP_QSTR_data' undeclared (first use in this function)
         } else if (op == MP_QSTR_data) {
                          ^
make: *** [build/WIPY/release/py/compile.o] Error 1
@danicampora - is there a way how to make it work?

Re: SyntaxError: invalid micropython decorator

Posted: Wed Oct 12, 2016 6:39 pm
by dhylands
You may need to do a clean after enabling those options.

The auto qstr generator seems to not always regenerate qstrs at the right time. Anytime I see any errors related to qstrs, I typically do a clean and then the problem goes away.

Re: SyntaxError: invalid micropython decorator

Posted: Wed Oct 12, 2016 8:58 pm
by danielm
Dave, it helped, thanks a lot!

Decorator @micropython.native increased performance of my function by almost 10%. I am looking for further improvements.

This is my code - its purpose is to render words consisting of letters - each represented by 20-30 X-Y coordinates defined as two-dimensional lists in a dictionary called "alphabet". I also tried array.array instead but it was not faster.

Code: Select all

	@micropython.native
	def renderTest(self, text, factor, times):
		count = 0
		data_array = bytearray(2)
		start = time.time()

		for x in range(times):
			for letterIndex in range(len(text)):
				for pointIndex in range(len(alphabet[text[letterIndex]])):

					data=self.currentX+factor*alphabet[text[letterIndex]][pointIndex][1] + 12288
					data_array[1],data_array[0] = data.to_bytes(2)
					self.spiNSS.value(0)
					self.spiWrite(data_array)
					self.spiNSS.value(1)
					data=self.currentY+factor*alphabet[text[letterIndex]][pointIndex][0] + 45056
					data_array[1],data_array[0] = data.to_bytes(2)
					self.spiNSS.value(0)
					self.spiWrite(data_array)
					self.spiNSS.value(1) 
					count+=1
					
				self.currentY+=factor*18
			self.currentY = 0
		print(time.time()-start)
		print(count)

I am manipulating with CS/NSS pin manualy. If this operation would not be required performance could be increased by approx. 10%. However that would require to implement CS toggling on C module level as it was discussed several months ago in another thread.

Is there any faster way how to perform int to (2)byte conversion in reversed order?

Could SPI.write() and Pin.value() be performed with @micropython.viper decorator?

EDIT: I just discovered that following two calculations are consuming 50% of the runtime:

Code: Select all

data=self.currentX+factor*alphabet[text[letterIndex]][pointIndex][1] + 12288
data=self.currentY+factor*alphabet[text[letterIndex]][pointIndex][0] + 45056
Maybe I could put them in separate function with @micropython.viper decorator?

Re: SyntaxError: invalid micropython decorator

Posted: Thu Oct 13, 2016 6:26 am
by pythoncoder
Looking at the innermost loop you're doing repeated lookups which could be cached, something along these lines

Code: Select all

for pointIndex, atli in enumerate(alphabet[text[letterIndex]]):
    atli_pi = atli[pointIndex]
    data=self.currentX + factor * atl_pi[1] + 12288
    data_array[1], data_array[0] = data.to_bytes(2)
    self.spiNSS.value(0)
    self.spiWrite(data_array)
    self.spiNSS.value(1)
    data=self.currentY + factor * atli_pi[0] + 45056
    data_array[1],data_array[0] = data.to_bytes(2)
    self.spiNSS.value(0)
    self.spiWrite(data_array)
    self.spiNSS.value(1)
    count+=1
I suspect the first two lines can be reduced to one but I can't get my head round it at 7:20am...

Re: SyntaxError: invalid micropython decorator

Posted: Wed Oct 19, 2016 4:16 pm
by danielm
@pythoncoder, I wanted to implement your solution, however enumerate() probably does not work with list of lists as expected:

Code: Select all

>>> alphabet["A"]
[[0, 0], [1, 4], [2, 8], [3, 12], [4, 16], [5, 20], [6, 24], [7, 28], [8, 32], [9, 28], [10, 24], [11, 20], [12, 16], [13, 12], [10, 12], [6, 12], [13, 12], [14, 8], [15, 4], [16, 0]]
>>> for pointIndex, atli in enumerate(alphabet["A"]):
...     print(atli[pointIndex])
...
...
...
0
4
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
IndexError: index out of range

Re: SyntaxError: invalid micropython decorator

Posted: Wed Oct 19, 2016 6:43 pm
by deshipu
"atli" are the individual elements there, and since they only have 2 items each, it's natural you get IndexError.