Skip to content

Commit

Permalink
Merge pull request #1131 from herbie-fp/simplify-server
Browse files Browse the repository at this point in the history
Refactor the JSON conversion and other helper functions in the server
  • Loading branch information
pavpanchekha authored Jan 20, 2025
2 parents 455c599 + 17d17eb commit 42d9ae4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 90 deletions.
5 changes: 2 additions & 3 deletions src/api/demo.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,8 @@
(define resp
(with-handlers ([exn:fail? (λ (e) (hash 'error (exn->string e)))])
(fn post-data)))
(if (hash-has-key? resp 'error)
(eprintf "Error handling request: ~a\n" (hash-ref resp 'error))
(eprintf "Success handling request\n"))
(when (hash-has-key? resp 'error)
(eprintf "Error handling request: ~a\n" (hash-ref resp 'error)))
(if (hash-has-key? resp 'error)
(response 500
#"Bad Request"
Expand Down
128 changes: 41 additions & 87 deletions src/api/server.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,30 @@
#:when (equal? (hash-ref result 'command) "improve"))
(get-table-data-from-hash result (make-path job-id)))])))

(define (get-json-converter command)
(match (herbie-command-command command)
['alternatives make-alternatives-result]
['cost make-cost-result]
['errors make-error-result]
['evaluate make-calculate-result]
['exacts make-exacts-result]
['explanations make-explanation-result]
['improve make-improve-result]
['local-error make-local-error-result]
['sample make-sample-result]
[_ (error 'compute-result "unknown command ~a" command)]))

(define (herbie-do-server-job command job-id)
(define herbie-result (wrapper-run-herbie command job-id))
(match-define (job-result kind test status time _ _ _ backend) herbie-result)
(match kind
['alternatives (make-alternatives-result herbie-result test job-id)]
['evaluate (make-calculate-result herbie-result job-id)]
['cost (make-cost-result herbie-result job-id)]
['errors (make-error-result herbie-result job-id)]
['exacts (make-exacts-result herbie-result job-id)]
['improve (make-improve-result herbie-result test job-id)]
['local-error (make-local-error-result herbie-result job-id)]
['explanations (make-explanation-result herbie-result job-id)]
['sample (make-sample-result herbie-result test job-id)]
[_ (error 'compute-result "unknown command ~a" kind)]))
(define basic-output ((get-json-converter command) herbie-result job-id))
;; Add default fields that all commands have
(hash-set* basic-output
'command
(~a (herbie-command-command command))
'job
job-id
'path
(make-path job-id)))

(define completed-work (make-hash))

Expand Down Expand Up @@ -179,11 +189,6 @@
(define manager #f)
(define manager-dead-event #f)

(define (get-command herbie-result)
; force symbol type to string.
; This is a HACK to fix JSON parsing errors that may or may not still happen.
(~s (job-result-command herbie-result)))

(define (compute-job-id job-info)
(sha1 (open-input-string (~s job-info))))

Expand Down Expand Up @@ -363,76 +368,36 @@
(place-channel-put manager (list 'finished manager worker-id job-id out-result)))

(define (make-explanation-result herbie-result job-id)
(hasheq 'command
(get-command herbie-result)
'explanation
(job-result-backend herbie-result)
'job
job-id
'path
(make-path job-id)))
(hasheq 'explanation (job-result-backend herbie-result)))

(define (make-local-error-result herbie-result job-id)
(hasheq 'command
(get-command herbie-result)
'tree
(job-result-backend herbie-result)
'job
job-id
'path
(make-path job-id)))

(define (make-sample-result herbie-result test job-id)
(hasheq 'tree (job-result-backend herbie-result)))

(define (make-sample-result herbie-result job-id)
(define test (job-result-test herbie-result))
(define pctx (job-result-backend herbie-result))
(define repr (context-repr (test-context test)))
(hasheq 'command
(get-command herbie-result)
'points
(pcontext->json pctx repr)
'job
job-id
'path
(make-path job-id)))
(hasheq 'points (pcontext->json pctx repr)))

(define (make-calculate-result herbie-result job-id)
(hasheq 'command
(get-command herbie-result)
'points
(job-result-backend herbie-result)
'job
job-id
'path
(make-path job-id)))
(hasheq 'points (job-result-backend herbie-result)))

(define (make-cost-result herbie-result job-id)
(hasheq 'command
(get-command herbie-result)
'cost
(job-result-backend herbie-result)
'job
job-id
'path
(make-path job-id)))
(hasheq 'cost (job-result-backend herbie-result)))

(define (make-error-result herbie-result job-id)
(define errs
(for/list ([pt&err (job-result-backend herbie-result)])
(define pt (first pt&err))
(define err (second pt&err))
(list pt (format-bits (ulps->bits err)))))
(hasheq 'command (get-command herbie-result) 'points errs 'job job-id 'path (make-path job-id)))
(hasheq 'points errs))

(define (make-exacts-result herbie-result job-id)
(hasheq 'command
(get-command herbie-result)
'points
(job-result-backend herbie-result)
'job
job-id
'path
(make-path job-id)))

(define (make-improve-result herbie-result test job-id)
(hasheq 'points (job-result-backend herbie-result)))

(define (make-improve-result herbie-result job-id)
(define test (job-result-test herbie-result))
(define ctx (context->json (test-context test)))
(define backend (job-result-backend herbie-result))
(define job-time (job-result-time herbie-result))
Expand All @@ -447,9 +412,7 @@
['timeout #f]
['failure (exception->datum backend)]))

(hasheq 'command
(get-command herbie-result)
'status
(hasheq 'status
(job-result-status herbie-result)
'test
test
Expand All @@ -464,11 +427,7 @@
'profile
profile
'backend
backend-hash
'job
job-id
'path
(make-path job-id)))
backend-hash))

(define (backend-improve-result-hash-table backend repr test)
(define pcontext (improve-result-pctxs backend))
Expand Down Expand Up @@ -529,8 +488,9 @@
(define (repr->json repr)
(hasheq 'name (representation-name repr) 'type (representation-type repr)))

(define (make-alternatives-result herbie-result test job-id)
(define (make-alternatives-result herbie-result job-id)

(define test (job-result-test herbie-result))
(define vars (test-vars test))
(define repr (test-output-repr test))

Expand Down Expand Up @@ -559,17 +519,11 @@
(define derivations
(for/list ([altn altns])
(render-json altn processed-pcontext test-pcontext (test-context test))))
(hasheq 'command
(get-command herbie-result)
'alternatives
(hasheq 'alternatives
fpcores
'histories
histories
'derivations
derivations
'splitpoints
splitpoints
'job
job-id
'path
(make-path job-id)))
splitpoints))

0 comments on commit 42d9ae4

Please sign in to comment.