public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* infix precedence optimizer for Scheme+ for Kawa
@ 2023-11-29  9:02 Damien Mattei
  0 siblings, 0 replies; only message in thread
From: Damien Mattei @ 2023-11-29  9:02 UTC (permalink / raw)
  To: kawa mailing list

[-- Attachment #1: Type: text/plain, Size: 4116 bytes --]

hello,

i have added optimisation features for infix precedence (
https://github.com/damien-mattei/Scheme-PLUS-for-Kawa/blob/main/optimize-infix.scm)
for the kawa parser version of Scheme+ (
https://github.com/damien-mattei/Scheme-PLUS-for-Kawa/blob/main/curly-infix2prefix4kawa.scm
)

for example for a given code in Scheme+ like this:

; modify coefficients layer(define (modification_des_poids M_i_o η
z_input z_output z̃_output ᐁ_i_o მzⳆმz̃) ; derivative of activation
function of the layer           ; the length of output and input layer
with coeff. used for bias update      {(len_layer_output
len_layer_input_plus1forBias) <+ (dim-matrix M_i_o)} ; use values and
define-values to create bindings              {len_layer_input <+
{len_layer_input_plus1forBias - 1}}      (for-each-in (j (in-range
len_layer_output)) ; line        (for-each-in (i (in-range
len_layer_input)) ; column , parcours les colonnes de la ligne sauf le
bias            {M_i_o[j {i + 1}]  <-  M_i_o[j {i + 1}] - (- η) *
z_input[i] * მzⳆმz̃(z_output[j] z̃_output[j]) * ᐁ_i_o[j]})        ;
and update the bias                {M_i_o[j 0]  <-  M_i_o[j 0] - (- η)
* 1.0 * მzⳆმz̃(z_output[j] z̃_output[j]) * ᐁ_i_o[j]}))

without optimization it is transformed after parsing in:

(define (modification_des_poids M_i_o η z_input z_output z̃_output
ᐁ_i_o  მzⳆმz̃) (<+ (len_layer_output len_layer_input_plus1forBias)
(dim-matrix M_i_o)) (<+ len_layer_input (-
len_layer_input_plus1forBias 1)) (for-each-in (j (in-range
len_layer_output))  (for-each-in (i (in-range len_layer_input))
($nfx$ (bracket-apply M_i_o j (+ i 1)) <- (bracket-apply M_i_o j (+ i
1)) -    (- η) * (bracket-apply z_input i) *    (მzⳆმz̃ (bracket-apply
z_output j) (bracket-apply z̃_output j)) *    (bracket-apply ᐁ_i_o
j)))  ($nfx$ (bracket-apply M_i_o j 0) <- (bracket-apply M_i_o j 0) -
(- η) * 1.0   * (მzⳆმz̃ (bracket-apply z_output j) (bracket-apply
z̃_output j)) *   (bracket-apply ᐁ_i_o j))))

this cause the $nfx$ algorithm to be executed at runtime.

for example in the above code

{M_i_o[j {i + 1}]  <-  M_i_o[j {i + 1}] - (- η) * z_input[i] *
მzⳆმz̃(z_output[j] z̃_output[j]) * ᐁ_i_o[j]}

at each iteration of loop  $nfx$ is called with its arguments:


   ($nfx$ (bracket-apply M_i_o j (+ i 1)) <- (bracket-apply M_i_o j (+
i 1)) -    (- η) * (bracket-apply z_input i) *    (მzⳆმz̃
(bracket-apply z_output j) (bracket-apply z̃_output j)) *
(bracket-apply ᐁ_i_o j))


Now the infix parser, before compilation produce a code like this,
where $nfx$ calls are replaced by the symbolic expression

according to the operator precedence rules:



(define (modification_des_poids M_i_o η z_input z_output z̃_output
ᐁ_i_o  მzⳆმz̃) (<+ (len_layer_output len_layer_input_plus1forBias)
(dim-matrix M_i_o)) (<+ len_layer_input (-
len_layer_input_plus1forBias 1)) (for-each-in (j (in-range
len_layer_output))  (for-each-in (i (in-range len_layer_input))   (<-
(bracket-apply M_i_o j (+ i 1))    (- (bracket-apply M_i_o j (+ i 1))
   (* (- η)      (* (bracket-apply z_input i)       (* (მzⳆმz̃
(bracket-apply z_output j) (bracket-apply z̃_output j))
(bracket-apply ᐁ_i_o j)))))))  (<- (bracket-apply M_i_o j 0)   (-
(bracket-apply M_i_o j 0)    (* (- η)     (* 1.0      (* (მzⳆმz̃
(bracket-apply z_output j) (bracket-apply z̃_output j))
(bracket-apply ᐁ_i_o j))))))))


so for the above expressions the simplest scheme code is evaluated at runtime:


   (<- (bracket-apply M_i_o j (+ i 1))    (- (bracket-apply M_i_o j (+
i 1))     (* (- η)      (* (bracket-apply z_input i)       (* (მzⳆმz̃
(bracket-apply z_output j) (bracket-apply z̃_output j))
(bracket-apply ᐁ_i_o j))))))


the performance at runtime in mathematic computation algorithms are
now _twice_ faster just with this optimization.

Next optimization will concern bracket-apply but this is a big work,
inference type need to be performed on many expressions  of all files
of code..

Damien

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-11-29  9:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-29  9:02 infix precedence optimizer for Scheme+ for Kawa Damien Mattei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).