Fork me on GitHub
#clojure
<
2023-03-31
>
Noah Bogart13:03:22

I have a function that takes an object and returns a keyword of its "simple type", where (1 2 3) is :list and true is :boolean and both 1 and 1/2 are :number . I've implemented it using cond and the built in functions because I couldn't get case to work (`type` returns the class object, not a symbol). is there a better way than this? function impl in thread

Noah Bogart13:03:27

(defn simple-type
  [elem]
  (cond
    ; literals
    (nil? elem) :nil
    (boolean? elem) :boolean
    (char? elem) :char
    (number? elem) :number
    (keyword? elem) :keyword
    (string? elem) :string
    (symbol? elem) :symbol
    ; reader macros
    (map? elem) :map
    (set? elem) :set
    (vector? elem) :vector
    (seq? elem) :list
    :else (type elem)))

p-himik13:03:29

condp could be used to avoid repeating elem everywhere.

Noah Bogart13:03:25

I don't think so cuz condp puts the predicate in the first position: (condp elem nil? :nil) becomes (if (elem nil?) :nil)

p-himik13:03:27

(condp #(%2 %1) ...)

👍 2
borkdude13:03:35

@UEENNMX0T I have a similar function in clj-kondo

Noah Bogart13:03:05

ha of course you do. i wonder how close ours are to each other

borkdude13:03:12

pretty similar I think

Andrew Ni14:03:44

I was trying to embed PyTorch into a clojure project using libpython-clj, but I keep getting segmentation errors when I try to train models. This is my code:

(ns test
  (:require [libpython-clj2.require :refer [require-python]]
            [libpython-clj2.python :as py :refer [py. py.. py.-]]))

(require-python '[torch.nn :as nn]
                '[torch])

(let [batch 32
      dim-in 10
      dim-h 50
      dim-out 10
      input-X (torch/randn batch, dim-in)
      output-Y (torch/randn batch, dim-out)
      model (nn/Sequential (nn/Linear dim-in, dim-h)
                           (nn/ReLU)
                           (nn/Linear dim-h, dim-out))
      loss-fn (nn/MSELoss)]
  (let [pred-y (model input-X)
        loss (loss-fn pred-y output-Y)]
    (py. loss backward)))
Does anyone know what's going on?

Andrew Ni14:03:08

This is the error message:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007ff804117589, pid=28256, tid=37891
#
# JRE version: OpenJDK Runtime Environment Homebrew (19.0.2) (build 19.0.2)
# Java VM: OpenJDK 64-Bit Server VM Homebrew (19.0.2, mixed mode, emulated-client, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, bsd-amd64)
# Problematic frame:
# 
C  [libsystem_pthread.dylib+0x1589]  pthread_mutex_lock+0x4
I have also had error logs where the problematic frame was # C [libpython3.9.dylib+0x1692de] take_gil+0x3e, but since take_gil appears in the stacktrace of mutex_lock it seems like it's the same error. I've found that it's the (py. loss backward) expression that causes the error. I'm using a conda venv for the python side with pytorch 1.12.1, and I have libpython-clj 2.024. Since it seems like this is an issue with the global interpreter lock and thread safety, I tried wrapping my code in each of the four variations of py/with-gil, but none of those worked. Finally, translating the code to python and running it in the same venv works fine.

Carlo21:04:48

Hmm, you probably should post this in #CLR5FD4ET too @U05021LUYL8

Carlo21:04:17

Given the fact you included a very precise repro case, I tried it, and it's working correctly on my end (it produces nil because that's the result of backward

Carlo22:04:08

Maybe tell us more on how you are building your environment, in #CLR5FD4ET

Ítallo Rian18:03:57

how can i set enviroment variable using lein to execute my project? without docker or any machine...

Ítallo Rian18:03:13

i have this code: (System/getenv "DATABASE_URL"), but i need know how set DATABASE_URL in enviroment

Ítallo Rian18:03:20

i tried add .ENV file, but it did not work :((((

Ítallo Rian18:03:47

lein ring server-headless 5000 DATABASE_URL=something doesn't work either

andre18:03:14

Try export DATABASE_URL=something before running lein

andre18:03:57

This env var will be valid only for the current terminal session, so you might want to use some other tool to manage env vars, like https://direnv.net/

dpsutton18:03:58

DATABASE_URL=something lein ring server-headless 5000

dpsutton18:03:28

you set the env stuff at the beginning. as posted there that is an argument to lein ring server

Ítallo Rian19:03:27

I tried the suggestion and got this: The term 'DATABASE_URL=something' is not recognized as a cmdlet name

Ítallo Rian19:03:38

i'm using windows OS

dpsutton19:03:54

oh. i have no idea how to set stuff on windows

Ítallo Rian19:03:15

export DATABASE_URL=something had the same problem

andre19:03:19

set DATABASE_URL=something should do the trick then

andre19:03:09

But it is only valid for the current session, using a separate tool to manage those env vars or putting on the lein command would be better... Not sure how to do any of those on windows tho

Ítallo Rian19:03:28

makes sense, I'll try

Ítallo Rian19:03:42

it's okay to be only in the session in my case...

andre19:03:05

Maybe creating a .bat that sets everything and starts the server is an option too

Ítallo Rian19:03:52

set DATABASE_URL=something it didn't work correctly, it's not picking up the application's System/getenv

andre19:03:34

What's the output of echo %DATABASE_URL%?

Ítallo Rian19:03:39

to add variables in .bat it's like SET VAR=something?

Ítallo Rian19:03:41

when i use echo, the variable appears correctly, weird

andre19:03:10

Sanity check, but you are running lein from the same terminal you ran set , right?

andre19:03:56

Try (System/getenv "PATH")

Ítallo Rian19:03:49

I think it worked here

Ítallo Rian19:03:10

It worked!

🙌 2
Ítallo Rian19:03:33

Thank you so much guys! You are very good.

Ítallo Rian19:03:14

what works is set DATABASE_URL=something but in regular command prompt not powershell

andre19:03:52

ohh... good to know. windows is weird sometimes 😕

andre19:03:36

you should give WSL a shot if you have not yet, windows always gave me headaches like this, no matter the language

Ítallo Rian19:03:41

really, i'm getting these headaches now while studying, for c# it's really good

Ítallo Rian19:03:05

I need to learn more about it before I switch entirely, thanks for the tip

andre19:03:51

yeah, C# being from microsoft works ok on windows, but I feel like windows is a 2nd class citizen for most(all?) other languages