12 ms·
Works nearly out of the box with llama.cpp, which makes it easy to try locally: https://github.com/ggerganov/llama.cpp/issues/2766 https://github.com/ggerganov/
by daemonologist 3y ago
Works nearly out of the box with llama.cpp, which makes it easy to try locally: https://github.com/ggerganov/llama.cpp/issues/2766 https://github.com/ggerganov/llama.cpp/issues/2766
Here's some output from q4_0 quantization of CodeLlama-7b-Python (first four lines are the prompt):
# prints the first ten prime numbers
def print_primes():
i = 2
num_printed = 0 # end of prompt
while num_printed < 10:
if is_prime(i):
print(i)
num_printed += 1
i += 1
def is_prime(n):
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
def main():
print_primes()
if __name__ == '__main__':
main()
It will be interesting to see how the larger models perform, especially after community tuning and with better context/prompting.
- blibble 3y agoI'd fail an interview candidate that suggested adding 1 each time for subsequent prime testing
- droopyEyelids 3y agoThe simple-to-understand, greedy algorithm is always the correct first choice till you have to deal with a constraint.
- blibble 3y agoit's not that though, there's several other typical optimisations in there just not the super obvious one that demonstrates extremely basic understanding of what a prime number is
- jpeterson 3y agoHaving "extremely basic understanding" of prime numbers immediately at one's command is important for approximately 0% of software engineering jobs. If you instant-fail a candidate for this, it says a lot more about you and your organization than the candidate.
- blibble 3y ago> If you instant-fail a candidate for this, it says a lot more about you and your organization than the candidate. yes, we expect professional software developers to have basic maths skills "what is a prime number" is taught to 7 year olds, it's not vector calculus what else would you consider to be an unreasonable thing for an employer to require? reading and writing skills of a typical 7 year old?
- ungruntled 3y agoI think the key problem here is that is is a bad programming question. If you know anything about prime numbers then coming up with an answer is trivial. If you expect a more optimized solution, then you are really only gauging the interviewee’s understanding of prime numbers. So effectively the interview is more about mathematics than it is about programming or problem solving.
- deleted 3y ago[deleted]
- daok 3y agoYou probably do not have a child of 7 years old because they do not know at that age what is a prime number. Second, basic math still that you never or rarely use or with very large time between usage might get rusty. You may understand the concept but not find the optimal solution. The way you are responding here shows quite a lot about how you are short sighted by instant-failing someone with a single question instead of trying to asses the whole person as much as you can. On you side, you are wasting opportunity to have a great person that could be a key player in your team by bringing other set of skill on the table.
- blibble 3y ago> You probably do not have a child of 7 years old because they do not know at that age what is a prime number. it's part of the curriculum for children of this age where I grew up (I did check) > The way you are responding here shows quite a lot about how you are short sighted by instant-failing someone with a single question instead of trying to asses the whole person as much as you can. On you side, you are wasting opportunity to have a great person that could be a key player in your team by bringing other set of skill on the table. it may also be the case that I have more in depth knowledge about the roles that I've interviewed candidates for most recently: hiring people to work for quants not instantly knowing that even numbers (other than 2) are not prime is a very strong signal
- noduerme 3y agoI'm mad at myself now that it has eaten 15 minutes of my time trying to come up with the right optimization. What's the trick? 2, +1, and then +2 from there on seems obvious but once you get to 9 is it worth building a list of nonprimes to skip?
- Our_Benefactors 3y agohttps://stackoverflow.com/a/54544012/1336678 https://stackoverflow.com/a/54544012/1336678 Common approach is to use square roots, this reduces the runtime. Recommend checking out project euler if you like solving hard math-code-o(n)-puzzles.
- noduerme 3y agoI didn't want to cheat by looking on S.O. but thanks ;) Yes it makes sense (in the GPT code) that you'd only go up to i * i ... although looking at pythonic while: statements is just gross to me in this context, it would feel a lot more readable to say, e.g. in PHP: for ($i=2;$i<sqrt($n);) { $i+=($i==2 ? 1 : 2); //although the first one should just be outside the loop }
- thewataccount 3y agoI think they're suggesting simply doing +2 +1 is not a good idea since ~half of all numbers are effectively non-prime simply by being even numbers. You can double the speed by using +2 without using any fancy tricks, just changing a single character.
- nojs 3y agoWell it doesn’t double the speed, since anything with a factor of 2 undergoes only one loop iteration inside is_prime. It basically just saves a function call
- dahfizz 3y agoThere's lots of more advanced optimizations, which would be an interesting avenue for discussion in an interview, but the drain dead algorithm would just use +2 instead of +1
- throwuxiytayq 3y agoi’d walk out from an interview that asked me to write a prime number generator
- belenos46 3y agoI've done that (maybe it was fizzbuzz, now that I'm thinking about it) and boy howdy does that get the people you're interviewing with agitated. Saying "I'm interviewing for a architect level container orchestration position. If I'm reinventing the wheel writing algorithms, something is terribly wrong" shuts them up, but doesn't make them any happier.
- zaphirplane 3y agoIs the job role just a for example ? what does and container orchestration architect do ? Something like this cluster should use envoy and Prometheus. The new clusters rate isn’t usually high enough for the stack to change. Real question I love these non conventional (swe, sre, pm, manager ) roles in tech
- deleted 3y ago[deleted]
- csmpltn 3y ago> "I'd fail an interview candidate that suggested adding 1 each time for subsequent prime testing" Congratulations! You must be that arrogant guy everybody hates interviewing with, the one with the superiority complex. How about instead of just failing people over literally nothing (wasting everybody's time and money) - just ask the candidate whether they could somehow reduce the search space by utilizing the properties of a prime number?
- hyperbovine 3y agoHow many even numbers are prime?
- tasubotadas 3y agoFinally we meet the lifeless drone that everybody complains about in the interviews. My suggestion for your next interview: decide to hire them just based on their leetcode score, but invite to the interview just to flex that you're still better at puzzle solving :-D Perfect
- internet101010 3y ago"Use <insert language> and optimize for runtime." <pastes question and constraints from leetcode> "solution begins with <insert default solution template from leetcode>". Copy solution from gpt, paste in leetcode, run, submit. "faster" Repeat. Repeat. Next question.
- maleldil 3y agoI assume you meant that you should add 2? If yes, that's such a mind boggling basic thing to do that I agree with you, and it makes no sense that you're being crucified.
- blibble 3y agoyes
- warent 3y agoit makes no sense that you're being crucified. Probably because there's significant overlap in the Venn diagram of people with years experience who professionally develop products that generate $millions in wealth/value, and people who would fail that interview. Or we have worked with junior developers who have really grown and flourished under our care, who would never have gotten that chance with such insane Draconian judgements. It's such an obvious "GOTCHA!!" setting someone up for failure. The way it's framed is very cringy because it signals that they don't care in their interviews about determining how objectively effective a software developer is.
- maleldil 3y agoI don't get it. This is an extremely basic fact that most people can figure out after thinking about primes for a minute. Maybe if you ask for "what's an easy optimisation here?" This would make the candidate think more closely about invariants that their code should hold, which in itself is a very valuable skill.
- NhanH 3y agoBecause I know enough not to write prime testing code that resemble anything like that loop to have to care about reducing its search space. If you actually want to test my knowledge about prime number, you can ask and I will tell you about using some probabilistic choice instead, and that I know fast deterministic one might exist, but I am not up to date on the state of the art. If I have to write the loop above, I am assuming it is the Fizzbuzz equivalent of your company to show that I know how to write a while loop. I am not thinking about reducing the search space because I am writing the code semi-unconscious and frankly just want to get to the next question.
- deleted 3y ago[deleted]
- deleted 3y ago[deleted]
- dontupvoteme 3y agoSimply prompting the output with "Optimize " prepended adds your suggestion, and some others.
- jckahn 3y agoSo I take it you typically produce fully optimized, thoughtful, and correct code on the first iteration while being actively judged by a stranger, yes?
- beanjuiceII 3y agoAlso don't these only ever need to be computed once
- squeaky-clean 3y agoYou'd reject a candidate that is willing and legally able to work for free while also cloning themselves so they can pair program with every one of your employees at once?
- 5Qn8mNbc2FNCiVV 3y agoI would not ship code just because of those reasons.
- deleted 3y ago[deleted]
- hititncommitit 3y ago> I'd fail an interview candidate that suggested adding 1 each time for subsequent prime testing The problem a team that always seeks the optimal solution is that never they get shit done. And that’s rarely optimal in a business context. Your view does not strike me to be nearly as arrogant as it is short-sighted. I think on a team of one I want the guy who gets it done without thinking. On a team of two I want a guy that’s somewhere in the middle. And on a team of three, that’s when I want my optimiser. Because in the time that guy number 3 has written let’s say 3 files of optimal code, guy number 10 files of not optimal code. And you rarely need guy number 3 to fix all ten, you just need him to fix the one or two files that actually matter. Clients rarely ask “is this optimal?”. But they always ask “is this done?”. All three developers have different approaches. All three are assets. I think on some level then you’re making the same mistake that we could say the “just add one guy” made if your comment is honest- not factoring in (his) speed. I think code readability, rather than code optimisation is far more important thing to get hung up on in an interview (and is, I must remind some of you, not to be confused with conciseness). And you can see this in the end result. But if you’re following along and the interviewee already knows you know what’s going on because of that, you can see it in smaller ways- it could be as simple as going back and changing a variable name such as open (maybe a function?) to isOpen (almost always a Boolean value). I think most of us are in this position pretty often where we’re writing and figuring out something at the same time, maybe we just see if it works first and don’t give a name much thought or maybe we change the actual value or type of a variable in the process and the variable name becomes ambiguous. I’d look for small indicators that shoe me that this person is considering readability. But I still don’t necessarily expect it to be as readable as it would be in a more natural setting I mean I think 90% of these sorts of things start off with the person saying “I swear I can type” within the first five minutes of being watched- if they get flustered while being watched that it effects their typing, it certainly also effects their coding as well.
- d0mine 3y agoSimple, concise, more efficient: def primes_upto(limit: int): """Generate prime numbers < *limit*.""" # Sieve of Eratosthene is_prime = [True] * limit for n in range(2, limit): if is_prime[n]: yield n # found prime number for c in range(n*n, limit, n): # start with square, less values are marked already is_prime[c] = False # mark composites if __name__ == "__main__": from itertools import islice print(*islice(primes_upto(100), 10)) # -> 2 3 5 7 11 13 17 19 23 29
- someplaceguy 3y agoYeah, but yours was generated by the "post unoptimized code to HN and wait for someone to optimize it" model, which, although free and doesn't require a GPU, is a much slower model.
- saurik 3y agoBut, unless you are trying to find a prime number low enough that you might as well look it up in a pre-generated table, it might still be end-to-end more efficient?
- someplaceguy 3y agoAh, good point :) Touché!
- turnsout 3y agoSomeone should turn this into a product! You highlight the code you want to optimize, and it posts it to hn as a semi-contextually-appropriate comment to invite code golfing, and the highest rated reply gets posted back to your repo as a PR.
- badloginagain 3y agoWe then train the LLM on all code optimized like this.
- 3y ago
- quickthrower2 3y agoFunny watching HN be nerd sniped by a machine :-)
- kilolima 3y agoFor printing the first 10 prime numbers, there's a one line solution to this problem: print("1, 2, 3, 5, 7, 11... and so on!
- kordlessagain 3y agoThat code shows me a parse error.
- tuukkah 3y agoThat can't be, because primes are numbers greater than 1.
- timjver 3y agoThey said nothing about not printing any non-primes.
- tuukkah 3y agoI suppose it's indeed possible to read the assignment as "print whatever you like as long as it includes 10 or more primes" but I'd fail you for that based on not being able to communicate with humans.
- imglorp 3y agoThis is a good point; English alone is an awful specification language. Everyone knows what you mean by "print the first ten prime numbers" but only by idiom and cultural context. This is why LLM is a good fit here, because -- when it works -- it includes all three aspects.
- FrozenSynapse 3y agohow much VRAM do you need to run quantised 7b model?
- RossBencina 3y agoRough calculation: typical quantization is 4 bit, so 7b weights fit in in 3.6GB, then my rule of thumb would be 2GB for the activations and attention cache (not usually quantized). So 6 or 8 GB VRAM would probably do it. llama.cpp will let you offload your choice of layers to GPU, so you could probably get quite a way with 4GB.
- rednab 3y agoInteresting here and in some of the other comments in this thread is that 1 is not a prime number ¹)! Now granted, that's more or less by definition and I don't doubt there's communities and fields where it is considered one, but still shows some of the subtleties at play when using language models. ¹) https://www.google.com/search?q=is+1+a+prime+number https://www.google.com/search?q=is+1+a+prime+number
- tuukkah 3y agoAlso interesting that nobody called out the AI's bullshit regarding is_prime: -1 True 0 True 1 True 2 True 3 True 4 False
- Renaud 3y agoWhether 1 should be a prime number or not wasn't clear-cut for centuries. Current consensus has settled on excluding 1 from the definition, but there are examples of publications well into the 20th century that still included 1 as prime. Fascinating read about the subject: https://arxiv.org/pdf/1209.2007.pdf https://arxiv.org/pdf/1209.2007.pdf
- orm 3y agoHow did you get access to the model? I have been waiting for weeks, and am still waiting, to get access to Llama2 (released a month+ ago), and access to this model goes through the same form, so I'm not very hopeful. Are you getting it from other methods?
- madduci 3y agoThey have updated their readme in the GitHub repository