Assignment Chef icon Assignment Chef

Browse assignments

Assignment catalog

33,401 assignments available

[SOLVED] Electrical Machine Design and Manufacturing WM983-15

Module title & code Electrical Machine Design and Manufacturing WM983-15 Assessment type Written report Weighting of mark The marking is done considering 100 marks are available. The submitted document will be marked holistically considering the module learning outcomes (LO2-6), and according to the following criteria: · Comprehension and analysis: 70 marks · Presentation: 30 marks See marking grid for more details. If this is a re-submission assessment, the combined mark will be capped at 50%, when all the LOs are at least partially met. The PMA is worth 80% of the module final mark. Assessment brief Vehicle 1. In your design, you should address the following issues: 1. Manufacturing Protocol: Design a manufacturing protocol for the motor you’ve designed. Discuss manufacturing challenges and quantify the impact of possible manufacturing issues on performance. 3. Reference Utilization: Leverage additional references, such as technical papers, to improve your work. Word count Word count only considers the number of words of the main body text. Tables, figures, headings, references, list of contents, list of abbreviations, list of figures, list of tables and appendix are not contributing to the word count. The submitted file should not exceed 30 pages. Inappropriate deviation of word counts, and penalisation is defined as follows: · The number of words of the PMA (3000 words) is within (+/-) 10% of the recommended word counts – no penalisation. · The number of words is between (+/-) 10% to 20% of the recommended word counts – 5 marks in Effort and Presentation shall be reduced. · The number of words is between (+/-) 20% to 30% of the recommended word counts – 10 marks in Effort and Presentation shall be reduced. · The number of words is over 30% of the recommended word counts – 15 marks in Effort and Presentation shall be reduced. Inappropriate usage of appendix or figures/tables (i.e., irrelevant, not referred to or not supporting the main text, or text repeatedly inserted into table, etc…) will be penalized in a similar manner as word count deviation. Module learning outcomes (numbered) LO1- Work in a team to differentiate electric machine technologies, considering their operation, characteristics, mechanical aspects, and cooling concepts [AHEP4;7, M2, M4] LO2- Evaluate and simulate the electromagnetic, mechanical, and thermal performance of electric machines by working on a group or individual project [AHEP4;7, M3, M4] LO3- Interpret the practicality of electric machine designs considering manufacturing processes and cost [AHEP4;7, M5, M7] LO4- Systematically design electric machines for hybrid/electric vehicles , with considerations of powertrain architecture, integration and influence of manufacturing [AHEP4;7, M1, M2] LO5- Critique design trade-offs and technological advances of electric machines for electric and hybrid vehicles [AHEP4;7, M4, M7] LO6- Select and critically evaluate technical literature and other sources of information to solve complex problems [AHEP4;7, M2, M4] Learning outcomes assessed in this assessment (numbered) LO2- Evaluate and simulate the electromagnetic, mechanical, and thermal performance of electric machines by working on a group or individual project [AHEP4;7, M3, M4] LO3- Interpret the practicality of electric machine designs considering manufacturing processes and cost [AHEP4;7, M5, M7] LO4- Systematically design electric machines for hybrid/electric vehicles , with considerations of powertrain architecture, integration and influence of manufacturing [AHEP4;7, M1, M2] LO5- Critique design trade-offs and technological advances of electric machines for electric and hybrid vehicles [AHEP4;7, M4, M7] LO6- Select and critically evaluate technical literature and other sources of information to solve complex problems [AHEP4;7, M2, M4] Marking guidelines See marking grid. Marking guidelines are in line with departmental policy and guidance for the level. https://warwick.ac.uk/fac/sci/wmg/globalcontent/general/postmodulework/pma/marks/ There are degrees of freedom on how to demonstrate the LOs and in which parts of your PMA. It is recommended to create a map of your contents and verify that each LO is addressed within the word count. Quality and depth should be prioritised over multiple repeats of the same investigations. In addition, please consider the following points: · Your assignment must be written in a technical/academic style. · The format of the presentation should be a single, concise document. · Relevant diagrams and calculations should be included. · Sanitisation of the results, particularly from lengthy or complex calculations, is strongly encouraged. high-quality referencesbeyond the taught material Academic guidance resources Refer to module materials. Writing your Post-Module Assignment (PMA): specific other advice for WMG's Postgraduate Taught Students As a postgraduate level student in WMG you may have some concerns about your ability to write at the high standard needed. This short guide is intended to supply general guidance and advice. It is important that if you have any questions, you discuss them with your module tutor. Remember, in writing your PMA you need to meet the expectations of the reader and University. A good PMA generally requires you to answer the question and to include… 1. A title, with your student number, module, lecturer's name and any other documentation required by the University. 2. A contents page and if proper, an abstract. 3. An introduction which acts as a 'map' to the rest of the document, describing the aim or purpose of the work and explaining how this aim is achieved. At this point it is usually helpful to paraphrase your conclusion. 4. Evidence of a suitable level of background reading of relevant texts. 5. Evidence of systematic and clear thinking, indicative of good planning and organisation. 6. Writing which makes sense, is clearly and carefully presented (proof-read and grammar checked). 7. A critical style. of writing which compares the main theories, concepts and arguments with conclusions that are based in evidence presented. 8. Important levels of exact academic referencing. 9. A logical and well-defined structure with headings and subheadings. 10. Clearly labelled and well-presented diagrams and other graphics that are discussed in the text. 11. Adherence to usual academic standards including length and a prompt submission. 12. A reference section in which every source that is cited in the text is listed.

$25.00 View

[SOLVED] National Branding through Food The Cultural Perceptions of Kimchi and Sushi Among Food Enthusias

National Branding through Food: The Cultural Perceptions of Kimchi and Sushi Among Food Enthusiasts Introduction Food has evolved into more than a necessity or form. of leisure over time and it has increasingly become a powerful medium through which nations communicate their identities and values to the world. As part of this cultural diplomacy, food plays as a symbol that conveys meanings shaped by historical, cultural, and political contexts (Sutton, 2001; Watson, 2006). In East Asia, Korean kimchi and Japanese sushi are two key cases of culinary symbols that have increasingly become recognized all over the world. This study investigates how these two food items are beneficial to the international image of their own countries Korea and Japan from the cultural perception and national branding’s perspective. This study aims to explore how they function within a broader framework of soft power and nation branding through revealing publicly available online content created by food enthusiasts. The charm of kimchi and sushi is deeply originated from how they are represented and perceived among international audiences. Sushi, often related to precision, elegance, and minimalism, always has been regarded as a symbol of Japanese refinement and cultural essence (Rath, 2010; Rockower, 2012). On the contrary, Kimchi represents bold flavors and collective heritage, which reflect Korea's resilience, communal spirit, and historical narrative (Park, Jeong, Lee, & Daily III, 2014; Patra, Das, Paramithiotis, & Shin, 2016). This study critically explores their semiotic and discursive constructions based on digital contents and then reveal the complicated role between food, identity, and nationhood. Research Objectives This study focuses on how two iconic national dishes—Korean kimchi and Japanese sushi—play as cultural symbols that is conductive to the soft power and international image of their respective nations. This study aims to realize how these foods are constructed and perceived symbolically and how these perceptions are connected with ideas of national identity and branding through analyzing digital content created by food enthusiasts. The three primary research objectives can be listed as follows. Research Objective 1: To explore how food enthusiasts perceive the symbolic meanings of kimchi and sushi aligned with Korean and Japanese national identities. Research Objective 2: To examine how online narratives promote the construction and reinforcement of national branding by food culture. Research Objective 3: To compare the transnational cultural flows and reception of kimchi and sushi on different digital platforms. Research Questions The three specific research question can be listed as follows. Research Question 1: How do food enthusiasts interpret the cultural symbolism of kimchi and sushi in online discourse? Research Question 2: How do online discussions in terms of kimchi and sushi construct or reveal the nation branding strategies of South Korea and Japan? Research Question 3: What similarities and differences can be observed in the cultural perception and reception of kimchi and sushi on different online content platforms? Significance of this study This study is beneficial to expand literature on food and cultural identity by illustrating how culinary practices and products serve as channels for symbolic communication. Furthermore, it involves the ongoing discussions in cultural studies, media studies, and international communication on the role of everyday cultural commodities in shaping ideas of national character. Central Theme of the Dissertation This study focuses on the symbolic power of food in the construction of national identity and the promotion of soft power in the global cultural marketplace. Based on kimchi and sushi as emblematic national dishes, this study analyzes how they are represented in digital content created by food enthusiasts and how their representations promote the cultural branding of Korea and Japan. It shows that food is a dynamic medium through which national narratives, identity claims, and cultural diplomacy are constructed and argued. Titles of Main Chapters and Outline of Their Content Chapter 1: Introduction This chapter includes the research topic, problem statement, research background, research questions, research objectives, significance of this study, and explains the rationale for focusing on kimchi and sushi. It stresses the significance of food as a medium in terms of cultural expression and introduces the concept of national branding (Sutton, 2001; Watson, 2006). Finally, the chapter also provides an overview of the structure and scope of this whole study. Chapter 2: Literature Review This chapter reviews main academic discussions on cultural branding, food and identity, and soft power in this research field and explores how researchers have conceptualized food as a marker of national identity and how it has been used in international image-making. The literature on kimchi and sushi as cultural products will be reviewed, which stresses the knowledge gaps in existing studies and positioning the current research among these scholarly conversations. Chapter 3: Research Methodology This study employs a qualitative discourse analysis approach to examine how kimchi and sushi are represented and shaped by food enthusiasts in digital environments. The study aims to reveal symbolic meanings, narrative structures, and value judgments conveyed through digital contents. This chapter will list the research design and research tools and especially specify the procedures of qualitative discourse analysis in this study. The researcher will explain why this method is appropriate for examining online cultural discourse and how the study will be carried on using publicly available data. Besides, it summarizes the data sources, sampling strategy, and ethical considerations and shows the usage of NVivo software for thematic analysis. Chapter 4: Data Collection and Analysis The study will collect a sample of 50–100 pieces of digital content posted between 2020 and 2025 created by food enthusiasts through purposive sampling and the researcher will present the data sources and details the process of collecting. Sources will be collected through keyword searches and hashtags aligned with kimchi, sushi, Korea, Japan, and national food. Screenshots, text extracts, and video captions will be saved in NVivo for data analysis. It includes a series of cases from blogs, Reddit threads, Instagram captions, Xiaohongshu posts, Bilibili videos, and digital food articles. The analysis will be presented based on those recurring themes including authenticity, heritage, global popularity, and national pride and the initial coding categories and representative cases will be illustrated. A thematic analysis will be employed using NVivo software to code patterns and emergent themes. Analytical categories include authenticity and heritage, modernity and innovation, cultural pride and ownership, global popularity and exoticism, identity and nationhood. The findings will be interpreted from the theoretical perspectives of national branding, representation, and globalization. Chapter 5: Findings and Discussion This chapter will introduce accordingly the main findings of the discourse analysis. It discusses how kimchi and sushi are conceptualized in terms of national culture, identity, and soft power. In addition to, it not only compares the similarities and differences in how Korea and Japan are represented by their culinary icons but also considers how digital contents showed from online communities contribute to the global circulation of cultural meanings. Chapter 6: Conclusion and Future Directions This chapter summarizes the main insights of the study and reflects on its implications in terms of cultural branding and transnational media discourse. At the same time, it also illustrates future research directions. For example, the examination of other national foods or the role of influencers in shaping food narratives. Theoretical Frameworks National Branding and Soft Power Soft power theory emphasizes how some countries project influence through cultural appeal rather than coercion (Nye, 2004; Anholt, 2007). National branding includes crafting a favorable international image employing cultural resources, such as food or other national cultural symbols. Cultural Representation and Identity The representation of food in media is helpful to construct national identity (Hall, 1997). This theoretical foundation aims to examine how discourses surrounding kimchi and sushi create meanings associated with Korea and Japan. Food and Cultural Globalization Food is not only a site of national pride but also a transnational cultural commodity (Appadurai, 1996; Heldke, 2015). These theories are conductive to analyze how food functions within global flows and how authenticity and exoticism are negotiated under the cross-cultural contexts in this study. Empirical Sources and Data The empirical data will consist of publicly accessible online digital content created by food enthusiasts. Approximately 50–100 data points will be collected from the following sources including Blogs (Food travel blogs and cooking blogs discussing kimchi and sushi), Reddit (Threads in subreddits such as r/food, r/Japan, r/Korea), Instagram (Public posts tagged with #kimchi, #sushi, #koreanfood, #japanesefood), Xiaohongshu (Reviews and photos posted by Chinese users), Bilibili (Commentary videos on Korean and Japanese cuisine). Digital food magazines and articles are originated from Articles from Bon Appétit, Eater, The Kitchn, and Korean/Japanese culinary websites. The digital content created by Food Enthusiasts in terms of Kimchi and Sushi will be selected using purposive sampling to ensure relevance, diversity, and richness of information. Ethical Considerations There are no direct participants in this research. Data will be collected from publicly accessible online content. The following ethical principles can be listed as follows. Only content posted voluntarily in digital space will be used. There are no content from private accounts, password-protected platforms, or subscription-only websites. All identifiable data including usernames or profile photos will be anonymized in the data analysis and final thesis. The whole study will comply with the University of Warwick’s ethical guidelines for digital research. Project Timeline Phase Estimated Date Literature Review 1 May – 30 May 2025 Data Collection 31 May – 20 June 2025 Data Analysis 20 June – 30 June 2025 Writing Draft Chapters 1 May – 30 June 2025 Final Editing and Submission 30 June – 5 July 2025 References Anholt, S. (2007). The theory of competitive identity. In Competitive Identity: The New Brand Management for Nations, Cities and Regions (pp. 25-42). London: Palgrave Macmillan UK. Appadurai, A. (1996). Modernity at large: Cultural dimensions of globalization (Vol. 1). Uiversity of Minnesota Press. Hall, S. (Ed.). (1997). Representation: Cultural representations and signifying practices (Vol. 2). Sage. Heldke, L. (2015). Exotic appetites: Ruminations of a food adventurer. Routledge. Nye, J. S. (2004). Soft power: The means to success in world politics. Public affairs. Park, K. Y., Jeong, J. K., Lee, Y. E., & Daily III, J. W. (2014). Health benefits of kimchi (Korean fermented vegetables) as a probiotic food. Journal of medicinal food, 17(1), 6-20. Patra, J. K., Das, G., Paramithiotis, S., & Shin, H. S. (2016). Kimchi and other widely consumed traditional fermented foods of Korea: a review. Frontiers in microbiology, 7, 1493. Rath, E. (2010). Food and fantasy in early modern Japan. Univ of California Press. Rockower, P. S. (2012). Recipes for gastrodiplomacy. Place Branding and Public Diplomacy, 8(3), 235-246. Sutton, D. E. (2001). Remembrance of repasts: an anthropology of food and memory. Berg. Watson, J. L. (Ed.). (2006). Golden arches east: McDonald's in East Asia. Stanford University Press.

$25.00 View

[SOLVED] COMP5425 Week11 Semester 1 2025

COMP5425 Week11 Semester 1, 2025 Conversational Retrieval  Introduction  Components  Benchmark and evaluation  Advance Topics  Applications Traditional vs Conversational Traditional  rely on keywords or short phrases Conversational  natural language dialogue interaction  enhancing the efficiency of information exchange and optimizing the user experience  Complex queries  Longer context Evolution of Search System Key word → NLP Based → Conversational  Examples  1966: ELIZA - One of the earliest examples of a natural language processing system.  2010s: Siri & Google Now - Introduction of voice-activated virtual assistants.  2018: BERT - A significant advancement in natural language understanding.  2020: TREC CAsT & WISE Datasets - Development of conversational search datasets.  2022: ChatGPT - A breakthrough in conversational AI Key Characteristics  Multi-turn  Context  Multi-turn context: Handling multiple turns in a conversation to maintain context.  Context understanding and management: Managing follow-up questions and ambiguous queries.  Knowledge integration and reasoning: Integrating knowledge to answer complex queries. Challenges  Context Understanding and Management  follow-up questions: “Could you give more details of it?” after a previous query.  Ambiguity and Under-Specified Queries  “Show me the best”  Mixed Task and Chat Intent  “Hi, how are you? Can you check my order?”  Knowledge Integration and Reasoning  “What is the most recent three PMs of Australia?” Trends  LLM integration  RAG  Multimodal Conversational Search  Domain Specific  Personalized  Etc…

$25.00 View

[SOLVED] Syllabus for BIOL 100 Principles of Biology 2024-2025

Biology Department Syllabus for BIOL 100: Principles of Biology Independent Study 2024-2025 Course Description Catalog Description Introduction to basic principles common to all facets of biology. Topics include a brief history of biology, the scientific method, the diversity of life, cell structure and reproduction, and metabolism. (Gen Ed: ST, GT-SC2) Additional Description Biology 100 is a one-semester survey course, designed to give students who are non-science majors a solid foundation in the basic principles and unifying concepts of biology. Students develop an understanding of science, the nature of scientific inquiry, and how evolution explains the unity and diversity of all life on Earth. The course focuses on common features that all organisms (living things) share, as well as their unique characteristics. Prerequisites None. Corequisites None. Recommend co-enrollment in BIOL 100L. Credit Hours and Expected Student Effort The course minimum is 2,250 minutes per credit hour. As an Independent Study correspondence course, the class is largely independent, and you are asked to take responsibility for your own learning. Your instructor is available for assistance, clarification, and feedback on material as needed, but the course is designed for you to independently read any required textbook and materials and progress through the course at a pace that is convenient and efficient for your schedule. Important Dates Students may drop courses before 15% of the course duration has passed from your registration date, without a record of the dropped course appearing on the student’s permanent record. When a student withdraws from a course before 75% of the course duration has passed, a grade of “W” (withdrawal) will be recorded on the academic record. After 75% of the course duration has passed, a student may not withdraw. Tuition and fees will not be adjusted for course withdrawals during this withdrawal period. Note: Drop/withdrawal dates for each course may vary. Please reach out to Independent Study Program staff for your specific dates. Those dates can also be found in your registration confirmation. Class Schedule Correspondence courses require self-motivation. We recommend that you create a schedule for yourself to keep up with the work to ensure your successful completion of the course. Program Student Learning Outcomes (SLOs) •   Students will develop a broad-based knowledge and application of concepts, techniques, and terminology in molecular, cellular, organismal, evolutionary, and ecological biology. •   Students will demonstrate critical thinking and problem-solving skills using experimental design and the scientific method. Course Objectives or Student Learning Outcomes/Instructional Methods Students will develop a broad-based knowledge of concepts and terminology in Biology as well as a supporting knowledge in the related field of chemistry. Student Learning Outcomes At the end of this course, students will: •   Explain the process of scientific inquiry (The Scientific Method). •   Examine and analyze key concepts of biology (e.g., cells, evolution, ecology, etc.). •   Distinguish between science and non-science/pseudoscience. •   Examine and analyze the core concepts of genetics and cell biology. •   Examine and interpret fundamentals ofthe evolutionary process and evolutionary change. •   Describe the diversity of life through review of the five kingdoms. General Education SLOs •   SLO 2: Quantitative Reasoning o Apply numeric, symbolic and geometric skills to formulate and solve quantitative problems. •    SLO 3: Inquiry & Analysis o Explore issues,, objects or works through the collection and analysis of evidence that results in informed conclusions or judgments and break complex topics or issues into parts to gain a better understanding of them. •    SLO 5: Critical Thinking o Comprehensive exploration of issues, ideas, artifacts, and events before accepting or formulating an opinion or conclusion. •    SLO 7: Information Literacy o Know when there is a need for information, to be able to identify, locate, evaluate, and effectively and responsibly use and share that information for the problem at hand. •    SLO 9: Problem Solving o Design, evaluate and implement a strategy to answer an open-ended question or achieve a desired goal. GT Pathways Designation and SLOs GT-SC2 This BIOL 100 course satisfies the Guaranteed Transfer (GT) Pathways Requirements for Natural & Physical Sciences The Colorado Commission on Higher Education has approved BIOL 100 for inclusion in the Guaranteed Transfer (GT) Pathways program in the GT-SC2 category. For transferring students, successful completion with a minimum C- grade guarantees transfer and application of credit in this GT Pathways category. For more information on the GT Pathways program, go to http://highered.colorado.gov/Academics/Transfers/gtPathways/curriculum.html. This designation verifies the following Content Criteria and Competencies are met in this course. GT-SC2 NATURAL & PHYSICAL SCIENCES CONTENT CRITERIA: Natural & Physical Sciences course are designed to develop students’ scientific literacy. Students should be able to: a. Develop foundational knowledge in specific field(s) of science. b. Develop an understanding of the nature and process of science. c. Demonstrate the ability to use scientific methodologies. d. Examine quantitative approaches to study natural phenomena. GTSC2 NATURAL & PHYSICAL SCIENCES COMPETENCIES AND STUDENT LEARNING OUTCOMES: Inquiry & Analysis Competency Inquiry is a systematic process of exploring issues/objects/works through the collection and analysis of evidence that results in informed conclusions/judgments. Analysis is the process of breaking complex topics or issues into parts to gain a better understanding of them. Student Learning Outcomes (SLOs 4, 5 & 6) Students should be able to: 4. Select or Develop a Design Process Select or develop elements of the methodology or theoretical framework to solve problems in a given discipline. 5. Analyze and Interpret Evidence a.   Examine evidence to identify patterns, differences, similarities, limitations, and/or implications related to the focus. b.   Utilize multiple representations to interpret the data. 6. Draw Conclusions State a conclusion based on findings. Quantitative Literacy Competency Competency in quantitative literacy represents a student’s ability to use quantifiable information and mathematical analysis to make connections and draw conclusions. Students with strong quantitative literacy skills understand and can create sophisticated arguments supported by quantitative evidence and can clearly communicate those arguments in a variety of formats (using words, tables, graphs, mathematical equations, etc.). Student Learning Outcomes (SLOs 1 & 2) Students should be able to: 1. Interpret Information: Explain information presented in mathematical forms (e.g., equations, graphs, diagrams, tables, words). 2. Represent Information:  Convert information into and between various mathematical forms (e.g., equations, graphs, diagrams, tables, words). Required Textbook(s) and Other Materials Your e-textbook (OpenStax Concepts of Biology, ISBN 1-938168-09-7) is free and a print version is available at very low cost! (The paperback is ISBN-10: 1506696538.)  The book is available (PDF format) in our Blackboard classroom or at https://openstax.org/details/books/concepts-biology Course Requirements This section provides an overview of the course requirements. A more detailed breakdown of the Course Agenda is at the end of the syllabus. It includes topics and expected student efforts, as well as the grading structure of the assignments. Please reach out to Independent Study Program staff for your specific dates and course completion deadline. Assignments Exams, Quizzes, and Projects All exams and assignments will be given a point value and your final grade will be calculated as  a percentage of the total available points (see "Grading" section below). The exams in the course are not proctored. Use of Technology (This section does not apply to print-based students.) Any course materials you need (beyond textbooks) are provided in Blackboard. Generative AI (ChatGPT, etc.): As generative artificial intelligence (AI) tools begin to transform. the way we use technology and work online, it’s important to recognize appropriate and inappropriate uses of AI within educational environments. In the Independent Study program, we value student learning, critical thinking, and authenticity. While AI can be a useful tool when brainstorming and generating ideas, it does not replace a student’s knowledge, voice, or ability to draw on individual experiences. When AI is used in place of a student’s authentic work, that usage not only impedes learning but in many cases is considered academic misconduct. To maintain academic integrity, we ask that students consult with their instructors before using AI in their courses and include proper attribution ifAI use is deemed acceptable. Sources utilized by AI should also be evaluated for credibility. Use of generative AI tools without proper attribution may result in a failed assignment, course, or other disciplinary action as determined by the instructor. Information for Print-Based Students All the materials you need will be provided. Handwritten materials that you submit must be legible. Print-based students will work with the required textbook and the syllabus. The following will also be provided in print form. •   Exams •   Tutorials •   Problem sets Attendance/Participation Due to the nature of correspondence courses, attendance is not required. Participation will be gauged based on investment and effort exhibited in course requirements. Grading This course uses a points-based system where each assignment and exam is given a point value. To calculate your grade total at any point follow the following equation: "Total pts earned" divided by "Total pts available (at that point)," and then multiply that by 100 = your grade percentage. The final grade is determined by adding up all the points earned and dividing that number by 610 (total points for the course and multiplying by 100 for a final percentage. A letter grade is assigned as follows: A = 90% - 100% B = 80% - 90% C = 70% - 80% D = 60% - 70% F < 60% Fractional grades will be rounded to the nearest whole number (89.5 will round to a 90%, whereas a 89.4 will round to 89%

$25.00 View

[SOLVED] ECON GU4415 - GAME THEORY Problem Set 3 Assignment 3 Q1 Games 4-5 Q3 Q6

ECON GU4415 - GAME THEORY Columbia University - Department of Economics Problem Set 3 Assignment 3: Q1 (Games 4-5), Q3, Q6 Due: June 10, Tuesday, 6pm 1. For each of the following normal-form. game below, find all Nash equilibria in pure and mixed strategies. Game 1: (1)/(2) C(q) D(1 − q) A (p) (2,1) (3,2) B(1 − p) (0,4) (4,3) Game 2: (1)/(2) L(q) R(1 − q) U(p) (3,3) (0,0) M(r) (1,1) (1,2) D(1 − p − r) (0,0) (3,3) Game 3: (1)/(2) L(q) R(1 − q) U(p) (2,1) (1,2) M(r) (1,2) (2,1) D(1 − p − r) (1,3) (3,1) Game 4: (1)/(2) L(q) R(1 − q) U(p) (4,3) (1,0) M(r) (3,1) (3,2) D(1 − p − r) (0,3) (4,0) Game 5: (1)/(2) X(q) Y(t) Z(1 − q − t) A(p) (3,3) (2,1) (1,2) B(r) (2,2) (3,0) (2,3) C(1 − p − r) (2,0) (2,1) (1,0) 2. Consider the two player normal form. game below. (2) L C R U (4, 2) (0, 0) (2, 3) (1) M (0, 2) (3, 4) (1, 1) D (1, 1) (2, 2) (1, 2) (a) Find the set of mixed strategy equilibria, if any, in which player 2 assigns zero probability to exactly one of her strategies. (b) Find the set of mixed strategy equilibria, if any, in which player 2 assigns positive probabilities to all of her strategy strategies. 3. Consider the two player normal form. game below. (2) L(q) C(t) R(1 − q − t) U(p) (3, 4) (1, 3) (0, y) (1) M(r) (2, 0) (0, 2) (1, 3) D(1 − p − r) (0, 1) (x, 0) (1, 3) (a) Find the set of values for x and y for which there is a mixed strategy Nash equilibrium where each player puts positive probability on each of her three pure strategies and player 2 plays C with exactly 1/3 probability? (b) Suppose x = 0 and y = 3. Find a mixed strategy Nash equilibrium where player 1 puts 0 probability on playing D. 4. Consider the following version of Matching Pennies game with two players, 1 and 2. Each player simul-taneously chooses from the set S = {1, ..., N}. If the players choose the same number then player 2 pays $1 to player 1 and the payoffs are (1, -1); otherwise no payment is made and the payoffs are (0, 0). Show that σ = ((1/N, ..., 1/N), (1/N, ..., 1/N)) is a mixed strategy Nash equilibrium. Is it unique? 5. Consider the following game between a police officer and a robber. The police officer (player 1) must decide whether to patrol the streets [P] or to hang out at the coffee shop [C]. His payoff from hanging out at the coffee shop is 10, while his payoff from patrolling the streets depends on whether he catches a robber (player 2). The robber must choose between prowling the streets [S] or staying hidden [H]. If the robber prowls the streets then the police officer catches him, and the police officer obtains a payoff of 20, while the robber ends up with -10. If the robber stays hidden, while the police officer patrols, then both player gets a payoff of 0. When the officer is at the coffee shop, robber gets 10 if he prowls the streets, and 0 if he stays hidden. What kind of game does this game look like? Find a mixed strategy Nash equilibrium of this game. 6. Consider the following market entry game. There are three firms, which are considering entering a new market. The payoff for each firm that enters is n/60, where n is the number of firms that enter. The cost of entering is 24. If a firm does not enter then its payoff is 11. Find the symmetric mixed-strategy equilibrium in which all three firms enter with the same probability, p. 7. Consider a simultaneous move game with 3 players, who choose between X and Y . That is, the strategy set for each player {X, Y}. The payoff of each player who selects X is 2mx − m2 x + 3, where mx is the number of players who choose X. The payoff of each player who selects Y is 4 − my, where my is the number of players who choose Y . Note that mx + my = 3. (a) Find the pure-strategy Nash equilibria, if any. (b) Determine whether this game has a symmetric mixed-strategy Nash equilibrium in which each player picks X with probability p. If you can find such an equilibrium, what is p?

$25.00 View

[SOLVED] Comp1210 – project: introduction to java

DeliverablesFiles to submit to Web-CAT: • MyLifeGoals.java • JLetter.javaSpecifications Overview: You will write two programs this week. One will print your short-term, medium-term, and long-term life goals to standard output, and the other will display letter J as a large block letter. Because these are small programs, you will only need to have 1-2 sentences of description in your class and method Javadoc comments (don’t forget the @author and @version tags in the class-level comment in each file).• MyLifeGoals.javaRequirements: Write the application MyLifeGoals that prints your name, your short-term, medium-term and long-term life goals.Design: Your program should contain a main method that prints the information listed under “Output” (i.e., your output should replace the text in Italics).Output: Your first and last name (separated by a space) (The second line should be blank) Describe your short-term life goals.(in one line) Describe your medium-term life goals. (in one line) Describe your long-term life goals. (in one line)Describe your life goals (if you have never thought about them, this is a good chance to think about them carefully). The actual output for each line of goals should be at least 100 characters not including spaces. Page 1 of Activity 1 shows how to break up a String literal in your program (where lines cannot exceed 80 characters) in order to print a longer line.Code and Test: The expected output for the program will vary from student to student, but it is important to follow output pattern described above, formatting requirements, and minimum character requirements. If you are not sure how many characters (no spaces) are in your output, you can copy your output into Microsoft Word then select the line (paragraph) of output. Then select the Word “Review” tab and click “Word Count” in the Proofing group on the far left.Page 1 of 2Project: Introduction to Java Page 2 of 2• JLetter.javaRequirements: Write the application JLetter that displays letter J as a large block letter composed of the characters ‘J’, ‘A’, ‘V’, ‘A’ arranged in a pattern with a width of 12 characters and a height of 10 lines. Each line should begin in column 1 with appropriate leading spaces in lines 3, 4, 5, and 6. Trailing spaces at the end of each line should be avoided.Design: Your program should contain a main method that prints exact the same pattern as shown in Figure 1. You should have 10 lines of output with each line in the pattern indicated.JAVAJAVAJAVA JAVAJAVAJAVA JAVA JAVA JAVA JAVA J JAVA JA JAVA JAVAJAVA JAVAJAFigure 1. Output from JLetter programCode and Test: The expected output contains 10 lines of letters in the specified order. Note that the first line of output has no leading spaces. Make sure that you print the pattern exactly as it appears above.Grading Web-CAT Submission: Prior to submitting to Web-CAT, you should complete both programs and make sure that they appear to be working correctly. You will have 10 attempts to receive full credit for your two program files, but you should try to get everything right on the first submission. You must submit both programs files at the same time. If you only submit one of the files, the submission will receive zero points for correctness. Creating a jGRASP Project and adding the two files will make Web-CAT submission more straightforward. [Instructions for creating a jGRASP project can be found in Activity 1 (p. 5-6).]Page 2 of 2

$25.00 View

[SOLVED] Comp1210 – project: introduction to java

DeliverablesFiles to submit to Web-CAT: • MyLifeGoals.java • JLetter.javaSpecifications Overview: You will write two programs this week. One will print your short-term, medium-term, and long-term life goals to standard output, and the other will display letter J as a large block letter. Because these are small programs, you will only need to have 1-2 sentences of description in your class and method Javadoc comments (don’t forget the @author and @version tags in the class-level comment in each file).• MyLifeGoals.javaRequirements: Write the application MyLifeGoals that prints your name, your short-term, medium-term and long-term life goals.Design: Your program should contain a main method that prints the information listed under “Output” (i.e., your output should replace the text in Italics).Output: Your first and last name (separated by a space) (The second line should be blank) Describe your short-term life goals.(in one line) Describe your medium-term life goals. (in one line) Describe your long-term life goals. (in one line)Describe your life goals (if you have never thought about them, this is a good chance to think about them carefully). The actual output for each line of goals should be at least 100 characters not including spaces. Page 1 of Activity 1 shows how to break up a String literal in your program (where lines cannot exceed 80 characters) in order to print a longer line.Code and Test: The expected output for the program will vary from student to student, but it is important to follow output pattern described above, formatting requirements, and minimum character requirements. If you are not sure how many characters (no spaces) are in your output, you can copy your output into Microsoft Word then select the line (paragraph) of output. Then select the Word “Review” tab and click “Word Count” in the Proofing group on the far left.Page 1 of 2Project: Introduction to Java Page 2 of 2• JLetter.javaRequirements: Write the application JLetter that displays letter J as a large block letter composed of the characters ‘J’, ‘A’, ‘V’, ‘A’ arranged in a pattern with a width of 12 characters and a height of 10 lines. Each line should begin in column 1 with appropriate leading spaces in lines 3, 4, 5, and 6. Trailing spaces at the end of each line should be avoided.Design: Your program should contain a main method that prints exact the same pattern as shown in Figure 1. You should have 10 lines of output with each line in the pattern indicated.JAVAJAVAJAVA JAVAJAVAJAVA JAVA JAVA JAVA JAVA J JAVA JA JAVA JAVAJAVA JAVAJAFigure 1. Output from JLetter programCode and Test: The expected output contains 10 lines of letters in the specified order. Note that the first line of output has no leading spaces. Make sure that you print the pattern exactly as it appears above.Grading Web-CAT Submission: Prior to submitting to Web-CAT, you should complete both programs and make sure that they appear to be working correctly. You will have 10 attempts to receive full credit for your two program files, but you should try to get everything right on the first submission. You must submit both programs files at the same time. If you only submit one of the files, the submission will receive zero points for correctness. Creating a jGRASP Project and adding the two files will make Web-CAT submission more straightforward. [Instructions for creating a jGRASP project can be found in Activity 1 (p. 5-6).]Page 2 of 2

$25.00 View

[SOLVED] Comp1210 – project: introduction to java

DeliverablesFiles to submit to Web-CAT: • MyLifeGoals.java • JLetter.javaSpecifications Overview: You will write two programs this week. One will print your short-term, medium-term, and long-term life goals to standard output, and the other will display letter J as a large block letter. Because these are small programs, you will only need to have 1-2 sentences of description in your class and method Javadoc comments (don’t forget the @author and @version tags in the class-level comment in each file).• MyLifeGoals.javaRequirements: Write the application MyLifeGoals that prints your name, your short-term, medium-term and long-term life goals.Design: Your program should contain a main method that prints the information listed under “Output” (i.e., your output should replace the text in Italics).Output: Your first and last name (separated by a space) (The second line should be blank) Describe your short-term life goals.(in one line) Describe your medium-term life goals. (in one line) Describe your long-term life goals. (in one line)Describe your life goals (if you have never thought about them, this is a good chance to think about them carefully). The actual output for each line of goals should be at least 100 characters not including spaces. Page 1 of Activity 1 shows how to break up a String literal in your program (where lines cannot exceed 80 characters) in order to print a longer line.Code and Test: The expected output for the program will vary from student to student, but it is important to follow output pattern described above, formatting requirements, and minimum character requirements. If you are not sure how many characters (no spaces) are in your output, you can copy your output into Microsoft Word then select the line (paragraph) of output. Then select the Word “Review” tab and click “Word Count” in the Proofing group on the far left.Page 1 of 2Project: Introduction to Java Page 2 of 2• JLetter.javaRequirements: Write the application JLetter that displays letter J as a large block letter composed of the characters ‘J’, ‘A’, ‘V’, ‘A’ arranged in a pattern with a width of 12 characters and a height of 10 lines. Each line should begin in column 1 with appropriate leading spaces in lines 3, 4, 5, and 6. Trailing spaces at the end of each line should be avoided.Design: Your program should contain a main method that prints exact the same pattern as shown in Figure 1. You should have 10 lines of output with each line in the pattern indicated.JAVAJAVAJAVA JAVAJAVAJAVA JAVA JAVA JAVA JAVA J JAVA JA JAVA JAVAJAVA JAVAJAFigure 1. Output from JLetter programCode and Test: The expected output contains 10 lines of letters in the specified order. Note that the first line of output has no leading spaces. Make sure that you print the pattern exactly as it appears above.Grading Web-CAT Submission: Prior to submitting to Web-CAT, you should complete both programs and make sure that they appear to be working correctly. You will have 10 attempts to receive full credit for your two program files, but you should try to get everything right on the first submission. You must submit both programs files at the same time. If you only submit one of the files, the submission will receive zero points for correctness. Creating a jGRASP Project and adding the two files will make Web-CAT submission more straightforward. [Instructions for creating a jGRASP project can be found in Activity 1 (p. 5-6).]Page 2 of 2

$25.00 View

[SOLVED] Comp1210 – project: introduction to java

DeliverablesFiles to submit to Web-CAT: • MyLifeGoals.java • JLetter.javaSpecifications Overview: You will write two programs this week. One will print your short-term, medium-term, and long-term life goals to standard output, and the other will display letter J as a large block letter. Because these are small programs, you will only need to have 1-2 sentences of description in your class and method Javadoc comments (don’t forget the @author and @version tags in the class-level comment in each file).• MyLifeGoals.javaRequirements: Write the application MyLifeGoals that prints your name, your short-term, medium-term and long-term life goals.Design: Your program should contain a main method that prints the information listed under “Output” (i.e., your output should replace the text in Italics).Output: Your first and last name (separated by a space) (The second line should be blank) Describe your short-term life goals.(in one line) Describe your medium-term life goals. (in one line) Describe your long-term life goals. (in one line)Describe your life goals (if you have never thought about them, this is a good chance to think about them carefully). The actual output for each line of goals should be at least 100 characters not including spaces. Page 1 of Activity 1 shows how to break up a String literal in your program (where lines cannot exceed 80 characters) in order to print a longer line.Code and Test: The expected output for the program will vary from student to student, but it is important to follow output pattern described above, formatting requirements, and minimum character requirements. If you are not sure how many characters (no spaces) are in your output, you can copy your output into Microsoft Word then select the line (paragraph) of output. Then select the Word “Review” tab and click “Word Count” in the Proofing group on the far left.Page 1 of 2Project: Introduction to Java Page 2 of 2• JLetter.javaRequirements: Write the application JLetter that displays letter J as a large block letter composed of the characters ‘J’, ‘A’, ‘V’, ‘A’ arranged in a pattern with a width of 12 characters and a height of 10 lines. Each line should begin in column 1 with appropriate leading spaces in lines 3, 4, 5, and 6. Trailing spaces at the end of each line should be avoided.Design: Your program should contain a main method that prints exact the same pattern as shown in Figure 1. You should have 10 lines of output with each line in the pattern indicated.JAVAJAVAJAVA JAVAJAVAJAVA JAVA JAVA JAVA JAVA J JAVA JA JAVA JAVAJAVA JAVAJAFigure 1. Output from JLetter programCode and Test: The expected output contains 10 lines of letters in the specified order. Note that the first line of output has no leading spaces. Make sure that you print the pattern exactly as it appears above.Grading Web-CAT Submission: Prior to submitting to Web-CAT, you should complete both programs and make sure that they appear to be working correctly. You will have 10 attempts to receive full credit for your two program files, but you should try to get everything right on the first submission. You must submit both programs files at the same time. If you only submit one of the files, the submission will receive zero points for correctness. Creating a jGRASP Project and adding the two files will make Web-CAT submission more straightforward. [Instructions for creating a jGRASP project can be found in Activity 1 (p. 5-6).]Page 2 of 2

$25.00 View

[SOLVED] Operating systems comp 310 – ecse 427 assignment #3: memory management

This is the final assignment. After implementing the memory manager, you will have built a simple, simulated OS. So far, our simulated OS supports simple Shell commands, and is capable to do process management according to different scheduling techniques. The assumption in the second assignment was that processes fully fit in the Shell memory (like what we saw in the Virtual Memory lecture). In this assignment, we will extend the simulation with demand paging. Similar to Assignment 2, this assignment is heavyweight. Plan your time wisely and don’t hesitate to ask questions on Discord if you get stuck. 1.1 Starter files description: You have two options: • [Recommended] Use your solution to Assignment 2 as starter code for this assignment. If your solution passes the public unit tests it is solid enough to use as a basis for the third assignment. • Use the official solution to Assignment 2 provided by the OS team as starter code. 1.2 Your tasks: Your tasks for this assignment are as follows: • Add scaffolding for paging. • Design and implement demand paging. • Implement the LRU replacement policy in demand paging. On a high level, in this assignment we will allow programs larger than the shell memory size to be run by our OS. We will split the program into pages; only the necessary pages will be loaded into memory and old pages will be switched out when the shell memory gets full. Programs executed through both the run and the exec commands need to use paging. In addition, we will further relax the assumptions of exec by allowing the same program to be executed multiple times by exec (remember that in Assignment 2 the programs run by exec needed to be different). For simplicity, in our tests for this assignment we will only consider the RR (round robin) policy, with a time slice of 2 instructions, with no multithreading and no background execution (the # parameter). However, the paging mechanism will work with the other scheduling policies as well, if it is implemented correctly. The API for exec does not change, so you still need to specifically pass the RR argument. More details on the behavior of your memory manager follow in the rest of this section. Even though we will make some recommendations, you have full freedom for the implementation. In particular: • Unless we explicitly mention how to handle a corner case in the assignment, you are free to handle corner cases as you wish, without getting penalized by the TAs. • You are free to craft your own error messages (please keep it polite).• Just make sure that your output is the same as the expected output we provide in the test cases in Section 2. • Formatting issues such as tabs instead of spaces, new lines, etc. will not be penalized. Let’s start coding! ☺ 1.2.1. Implement the paging infrastructure We will start by building the basic paging infrastructure. For this intermediate step, you will modify the run and exec commands to use paging. Note that, even if this step is completed successfully, you will see no difference in output compared to the run/exec commands in Assignment 2. However, this step is crucial, as it sets up the scaffolding for demand paging in the following section. As a reminder from Assignment 2, the run API is: run SCRIPT Executes the commands in the file SCRIPT run assumes that a file exists with the provided file name, in the current directory. It opens that text file and then sends each line one at a time to the interpreter. The interpreter treats each line of text as a command. At the end of the script, the file is closed, and the command line prompt is displayed. The exec API is: exec prog1 prog2 prog3 POLICY • As in Assignment 2, we will not be testing recursive exec calls. • Unlike Assignment 2, exec now supports running the same script (i.e., exec can take identical arguments so exec prog1 prog1 is a legal instruction). You will need to do the following to implement the paging infrastructure. 1. Setting up the backing store for paging. [OPTIONAL] A backing store is part of a hard disk that is used by a paging system to store information not currently in main memory. In this assignment, we will simulate the backing store as a directory, situated in the current directory. o An empty backing store directory is created when the shell is initialized. In case the backing store directory is already present (e.g., because of an abnormal shell termination) then the initialization removes all contents in the backing store. o The backing store is deleted upon exiting the shell with the quit command. If the Shell terminated abnormally without using quit then the backing store may still be present. o An explicit backing store is more realistic, and if you intend to develop your toy OS further (in particular, with the ability to write data into the pages rather than them being entirely read-only) then we strongly recommend setting up a backing store from the start. However, we will not evaluate your backing store directory, so you can simply treat the original script files themselves as the read-only backing store. 2. Set up process sharing. o When exec is passed the same script name twice, we will want to share memory between them. Modify your existing code loading setup from A2 so that two scripts with the same name can share their loaded code. If you loaded code contiguously, you have something like base+bounds virtual memory. Allow multiple processes with the same name to use the same base and bounds.3. Partitioning the Shell memory. The shell memory will be split into two parts: o Frame store. A part that is reserved to load pages into the shell memory. The number of lines in the frame store should be a multiple of the size of one frame. In this assignment, each page consists of 3 lines of code. Therefore, each frame in the frame store has 3 lines. If you implemented code memory separately from variable memory in A2, then there’s probably very little to do here. o The frame size of 3 is an unfortunate complication (a power of two would usually make more sense). We have set it up this way so that we can test interesting cases of demand paging without having you implement more scheduling policies. For address translation, you can will probably want to use the / and % operators to separate the page number and offset respectively. o Variable store. A part that is reserved to store variables. You are free to implement these two memory parts as you wish. For instance, you can opt to maintain two different data structures, one for loading pages and one for keeping track of variables. Alternatively, you can keep track of everything (pages + variables) in one data structure and keep track of the separation via the OS memory indices (e.g., you can have a convention that the last X lines of the memory are used for tracking variables). For now, the sizes of the frame store and variable store can be static. We will dynamically set their sizes at compile time in the next section. 4. Code loading. The shell will load script(s) into the frame memory as follows. o If you are using a backing store directory, the script(s) are copied into the backing store. The original script files (in the current directory) are closed, and the files in the backing store are opened. If exec has identical arguments, the program will be copied into the backing store just once. o You will use the files in the backing store to load program pages into the frame store. o At this point, you will load all the pages into the frame store for each program. This will change in the next section. Unlike Assignment 2, where you were encouraged to load the scripts contiguously into the Shell memory, in this assignment the pages do not need to be contiguously loaded. For example, you can load into memory as follows for 2 programs that each have 2 pages (i.e., 6 lines of code per program). Experiment with different orders to check that your paging implementation is working. Frame store 0 prog2-page0 1 prog2-page0 2 prog2-page0 3 prog1-page0 4 prog1-page0 5 prog1-page0 6 prog2-page1 7 prog2-page1 8 prog2-page1 9 prog1-page1 10 prog1-page1 11 prog1-page1 12o However, for grading purposes, when a page is loaded into the frame store, it must be placed in the first free spot (i.e., the first available hole). 5. Creating the page table. For each script, a page table needs to be added to its PCB to keep track of the loaded pages and their corresponding frames in memory. You are free to implement the page table however you wish. A possible implementation is adding a page table array, where the values stored in each cell of the array represent the frame number in the frame store. For instance, in the example above, the page tables would be: Prog 1: pagetable[0] = 1 //frame 1 starts at line 3 in the frame store pagetable[1] = 3 //frame 3 starts at line 9 in the frame store Prog 2: pagetable[0] = 0 //frame 0 starts at line 0 in the frame store pagetable[1] = 2 //frame 2 starts at line 6 in the frame store You may want to also keep validity information separately, or you could use an unreasonable frame number like -1 to indicate that an entry is invalid. Note that you will also need to modify the program counter to be able to navigate through the frames correctly. For instance, to execute prog 1, the PC needs to make the transitions between the 2 frames correctly, accessing lines: 3,4,5,9,10,11. Assumptions (for now): o The frame store is large enough to hold all the pages for all the programs. o The variable store has at least 10 entries. o An exec/run command will not allocate more variables than the size of the variable store. o Each command (i.e., line) in the scripts will not be larger than a shell memory line (i.e., 100 characters in the reference implementation). o A one-liner is considered as one command If everything is correct so far, your run/exec commandsshould have the same behavior as in Assignment 2. You can use the existing unit tests from Assignment 2 to make sure your code works correctly. 1.2.2. Extend the OS Shell with demand paging We are now ready to add demand paging to our shell. In Section 1.2.1, we assumed the all the pages of all the programs fit in the shell memory. Now, we will get rid of this assumption. 1. Setting shell memory size at compile time. First, you need to add two compilation flags to adjust the frame store size and variable store size at compile time as follows. o In gcc, you will need to use the -D compilation option, which replaces a macro by a value -D =. o In make, you can pass the value of the variable from the command line. Example: At the command line: make xval=42 In the Makefile: gcc -D XVAL=$(xval) -c test.cIn test.c: int x=XVAL; o Using the technique described above, your shell will be compiled by running make mysh framesize=X varmemsize=Y where X and Y represent the number of lines in the frame store and in the variable store. You can assume that X will always be a multiple of 3 in our tests and that X will be large enough to hold at least 2 frames for each script in the test. The name of the executable remains mysh. o Print the following message at shell startup, instead of the version message: ”Frame Store Size = X; Variable Store Size = Y” Where X and Y are the values passed to make from the command line. Please make sure your program compiles this way and that the memory sizes are adjusted. 2. Code loading. Unlike the previous Section, in this section the code pages will be loaded into the shell memory dynamically, as they become necessary. o In the beginning of the run/exec commands, only the first two pages of each program are loaded into the frame store. A page consists of 3 lines of code. In case the program is smaller than 3 lines of code, only one page is loaded into the frame store. Each page is loaded in the first available hole. o The programs start executing, according to the selected scheduling policy (in our case, RR with time slice of 2 lines of code). 3. Handling page faults. When a program needs to execute the next line of code that resides in a page which is not yet in memory, a page fault is triggered. Upon a page fault: o The current process P is interrupted and placed at the back of the ready queue, even if it may still have code lines left in its “time slice”. The scheduler selects the next process to run from the ready queue. o If you want your A3 solution to work with policies other than RR, you might not want to always place P at the back of the ready queue. Again, such implementation details are up to you! o The missing page for process P is brought into the frame store from the file in the backing store. P’s page table needs to be updated accordingly. The new page is loaded into the first free slot in the frame store if a free slot exists in the frame store. o If the frame store is full, we need to pick a victim frame to evict from the frame store. For now, pick a random frame in the frame store and evict it. We will adjust this policy in Section 1.2.3. Do not forget to update P’s page table. Upon eviction, print the following to the terminal: ”Page fault! Victim page contents:” ”End of victim page contents.” Upon page faults when the frame store is not full, print the following: ”Page fault!” o P will resume running whenever it comes next in the ready queue, according to the scheduling policy.o When a process terminates, you should not clean up its corresponding pages in the frame store. Note that, because the scripting language is very simple the pages can be loaded in order into the shell memory (i.e., for a program, you can assume that you will first load page 1, then pages 2, 3, 4 etc.). This greatly simplifies the implementation, but be aware that real paging systems also account for loops, jumps in the code etc. Also, our pages are always read-only, so you never have to worry about backtracking in the backing store to overwrite updated data. Obviously, real paging systems have to account for that as well. Also note that if the next page is already loaded in memory, there is no page fault. The execution simply continues with the instruction from the next page, if the current process still has remaining lines in its “time slice”. 1.2.3. Adding Page Replacement Policy The final piece is adjusting the page replacement policy to Least Recently Used (LRU). As seen in class, you will need to keep track of the least recently used frame in the entire frame store and evict it. You must implement accurate LRU, not an approximation. Note that, with this policy, a page fault generated by process P1 may cause the eviction of a page belonging to process P2. 2. TESTCASES We provide 5 testcases and expected outputs in the starter code repository. These are the public tests. There are hidden tests for this assignment. Please run the testcases to ensure your code runs as expected, and make sure you get similar results in the automatic tests. You are strongly encouraged to add more of your own tests as well. As with A2, you need to run the given test cases from the A3-2024/test-cases/ directory, and your code for the assignment should still be placed in A1-2024/starter-code. Apologies for the confusion. IMPORTANT: The grading infrastructure uses batch mode, so make sure your program produces the expected outputs when testcases run in batch mode. You can assume that the grading infrastructure will run one test at a time in batch mode, and that there is a fresh recompilation between two testcases. 3. WHAT TO HAND IN The assignment is due on November 22, 2024 at 23:59. Your final grade will be determined by running the code in the GitLab repository that is crawled by our grading infrastructure. We will take into account the most recent commit that happened before the deadline, on the main branch of your fork. In addition to the code, please include a README mentioning the author name(s) and McGill ID(s), any comments the author(s) would like the TA to see, and mention whether the code uses the starter code provided by the OS team or not. The project must compile on the our server by running make clean; make myshThe project must run in batch mode, i.e. ./mysh < testfile.txt Feel free to modify the Makefile to add more structure to your code, but make sure that the project compiles and runs using the commands above. Note: You must submit your own work. You can speak to each other for help but copied code will be handled as to McGill regulations. Submissions are automatically checked via plagiarism detection tools. 4. HOW IT WILL BE GRADED Your program must compile and run on our server to be graded. If the code does not compile/run using the commands in Section 3, in our grading infrastructure you will receive 0 points for the entire assignment. If you think your code is correct and there is an issue with the grading infrastructure, contact the instructor. Your assignment is graded out of 20 points. You were provided 5 testcases, with expected outputs. If your code matches the expected output, you will receive 2 points for each testcase. There are an additional 5 hidden test cases, and you will also receive 2 points for each of them. You will receive 0 points for each testcase where your output does not match the expected output. Differences in whitespace, such as tabs instead of spaces, new lines, etc. will not be penalized. The TA will look at your source code only if the program runs (correctly or not). The TA looks at your code to verify that you implemented the requirement as requested. Specifically: • Hardcoded solutions will receive 0 points for the hardcoded testcase, even if the output is correct. • Programming expectations. Your code needs to have a reasonable and consistent programming style. This includes effective comments explaining why various parts of the code are needed, and how those parts work. The TA may remove up to 4 points, as they see fit, if your style is not reasonable and consistent. • You must write this assignment in the C Programming language, otherwise the assignment will receive 0 points. Congratulations! If you made it this far, you have implemented a simple simulated Operating System ☺

$25.00 View

[SOLVED] Operating systems comp 310 – ecse 427 assignment #2: multi-process scheduling

1. Assignment Description This is the second of a series of three assignments that build upon each other. In this assignment, you will extend the simulated OS to support running concurrent processes. This assignment is longer than the first one, so please plan your time wisely, and don’t hesitate to ask questions on Discord if you get stuck. 1.1 Starter files description: You have three options: • [Recommended] Use your solution to Assignment 1 as starter code for this assignment. If your solution passes the Assignment 1 testcases, it is solid enough to use as a basis for the second assignment. • Use the official solution to Assignment 1 provided by the OS team as starter code. The solution will be released on approximately October 7, so you will have to wait to start programming. You can use this time to go over the assignment instructions carefully and sketch your solution. To obtain a local copy of this documentation, you can get the files from our git repository: $ git remote add staff https://gitlab.cs.mcgill.ca/mkopin/comp310-ecse427-coursework-f24 $ git fetch staff $ git checkout main $ git merge staff/main Additionally, if you did the bonus task for assignment 1, you should either move that work off of your main branch, or change the command name to something other than exec, as this assignment will have you implement a different exec command. 1.2 Your tasks: Your tasks for this assignment are as follows: • Remove the call to help from main so that the help text is no longer printed. • Implement the scheduling infrastructure. • Extend the existing OS Shell syntax to create concurrent processes. • Implement different scheduling policies for these concurrent processes. On a high level, in this assignment you will run concurrent processes via the exec command, and you will explore different scheduling strategies and concurrency control. Exec can take up to three files as arguments. The files are scripts which will run as concurrent processes. For each exec argument (i.e., each script), you will need to load the full script code into your shell memory. For this assignment, you can assume that the scripts will be short enough to fully fit into the shell memory – this will change in Assignment 3.You will also need to implement a few data structures to keep track of the code execution for the scripts, as the scheduler switches the processes in and out of the “running” state. After this infrastructure is set up, you will implement the following scheduling policies: FCFS, SJF, and RR. More details on the behavior of your scheduler follow in the rest of this section. Even though we will make some recommendations, you have full freedom for the implementation. In particular: • Unless we explicitly mention how to handle a corner case in the assignment, you are free to handle corner cases as you wish, without getting penalized by the TAs. • You are free to craft your own error messages (please keep it polite). • Just make sure that your output is the same as the expected output we provide in the test cases in Section 2. • Formatting issues such as tabs instead of spaces, new lines, etc. will not be penalized. Let’s start programming! ☺ 1.2.1. Implement the scheduling infrastructure We start by building the basic scheduling infrastructure. For this intermediate step, you will modify the run command to use the scheduler and run SCRIPT as a process. Note that, even if this step is completed successfully, you will see no difference in output compared to the run command in Assignment 1. However, this step is crucial, as it sets up the scaffolding for the exec command in the following section. As a reminder from Assignment 1, the run API is: run SCRIPT Executes the commands in the file SCRIPT run assumes that a file exists with the provided file name, in the current directory. It opens that text file and then sends each line one at a time to the interpreter. The interpreter treats each line of text as a command. At the end of the script, the file is closed, and the command line prompt is displayed once more. While the script executes, the command line prompt is not displayed. If an error occurs while executing the script due a command syntax error, then the error is displayed, and the script continues executing. You will need to do the following to run the SCRIPT as a process: 1. Code loading. Instead of loading and executing each line of the SCRIPT one by one, you will load the entire source code of the SCRIPT file into the OS Shell memory. It is up to you to decide how to encode each line in the Shell memory. o Hint: If you solved Section 1.2.1 in Assignment 1 correctly, it might come in handy for loading the source code into the Shell memory. o Hint: Alternatively, you may want to separate the shell memory for variables from a new data structure that you define for holding program lines. o Hint: While you could store the program lines in the PCB, that approach would not work for Assignment 3. Therefore, we recommend using a structure shared by all processes.2. PCB. Create a data-structure to hold the SCRIPT PCB. PCB could be a struct. In the PCB, at a minimum, you need to keep track of: o The process PID. Make sure each process has a unique PID. o The spot in the Shell memory where you loaded the SCRIPT instructions. For instance, if you loaded the instructions contiguously in the Shell memory (highly recommended), you can keep track of the start position and length of the script. o The current instruction to execute (i.e., serving the role of a program counter). 3. Ready Queue. Create a data structure for the ready queue. The ready queue contains the PCBs of all the processes currently executing (in this case, there will be a single process). One way to implement the ready queue is to add a next pointer in the PCB (which points to the next PCB in the ready queue), and a pointer that tracks the head of the ready queue. 4. Scheduler logic. If steps 1—3 were done correctly, we are now in good shape to execute SCRIPT through the scheduler. o The PCB for SCRIPT is added at the tail of the ready queue. Note that since the run command only executes one script at a time, SCRIPT is the only process in the ready queue (i.e., it is both the tail and the head of the queue). This will change in Section 1.2.2 for the exec command. o The scheduler runs the process at the head of the ready queue, by sending the process’ current instruction to the interpreter. o The scheduler switches processes in and out of the ready queue, according to the scheduling policy. For now, the scheduling policy is FCFS, as seen in class. o When a process is done executing, it is cleaned up (see step 5 below) and the next process in the ready queue starts executing. 5. Clean-up. Finally, after the SCRIPT terminates, you need to remove the SCRIPT source code from the Shell memory. Assumptions • The shell memory is large enough to hold three scripts and still have some extra space. In our reference solution, the size of the Shell memory is 1000 lines; each script will have at most 100 lines of source code. If you implemented your shell from scratch, please use the same limits. • You can also assume that each command (i.e., line) in the scripts will not be larger than 100 characters. If everything is correct so far, your run command should have the same behavior as in Assignment 1. You can use the existing unit tests from Assignment 1 to make sure your code works correctly. 1.2.2. Extend the OS Shell with the exec command We are now ready to add concurrent process execution in our shell. In this section, we will extend the OS Shell interface with the exec command: exec prog1 prog2 prog3 POLICY Executes up to 3 concurrent programs, according to a given scheduling policy • exec takes up to four arguments. The two calls below are also valid calls of exec: o exec prog1 POLICY o exec prog1 prog2 POLICY • POLICY is always the last parameter of exec. • POLICY can take the following three values: FCFS, SJF, RR, or AGING. If other arguments are given, the shell outputs an error message, and exec terminates, returning the command prompt to the user.Exec behavior for single-process. The behavior of exec prog1 POLICY is the same as the behavior of run prog1, regardless of the policy value. Note: this is not a special case – a correctly implemented exec should have this property. Use this comparison as a sanity check. Exec behavior for multi-process. Exec runs multiple processes concurrently as follows: • The entire source code of each process is loaded into the Shell memory. • PCBs are created for each process. • PCBs are added to the ready queue, according to the scheduling policy. For now, implement only the FCFS policy. • When processes finish executing, they are removed from the ready queue and their code is cleaned up from the shell memory. Assumptions • For simplicity, we are simulating a single core CPU. • We will not be testing recursive exec calls. That is, you can assume that a program does not contain other exec calls. • Each exec argument is the name of a different script filename. If two exec arguments are identical, the shell displays an error (of your choice) and exec terminates, returning the command prompt to the user (or keeps running the remaining instructions, if in batch mode). • If there is a code loading error (e.g., running out of space in the shell memory), then no programs run. The shell displays an error, the command prompt is returned, and the user will have to input the exec command again. Example execution prog1 code prog2 code prog3 code echo helloP1 set x 10 echo $x echo byeP1 echo helloP2 set y 20 echo $y print y echo byeP2 echo helloP3 set z 30 echo byeP3 Execution: $ exec prog1 prog2 prog3 FCFS helloP1 10 byeP1 helloP2 20 20 byeP2 helloP3 byeP3 $ //exec ends and returns command prompt to user 1.2.3. Adding Scheduling Policies Extend the scheduler to support the Shortest Job First (SJF) and Round Robin (RR) policies, as seen in class. • For SJF, use the number of lines of code in each program to estimate the job length.• For RR, schedulers typically use a timer to determine when the turn of a process ended. In this assignment, we will use a fixed number of instructions as a time slice. Each process gets to run 2 instructions before getting switched out. Example execution (prog1, prog2, prog3 code is the same as in Section 1.2.2) Example SJF Example RR $ exec prog1 prog2 prog3 SJF helloP3 byeP3 helloP1 10 byeP1 helloP2 20 20 byeP2 $ $ exec prog1 prog2 prog3 RR helloP1 helloP2 helloP3 10 byeP1 20 20 byeP3 byeP2 $ 1.2.4. SJF with job Aging One of the important issues with SJF is that short jobs continuously preempt long jobs, leading to starvation. Aging is a common technique that addresses this issue. In this final exercise, you will implement a simple aging mechanism to promote longer running jobs to the head of the ready queue. The aging mechanism works as follows: • Instead of sorting jobs by estimated job length, we will sort them by a “job length score”. You can keep track of the job length score in the PCB. • In the beginning of the exec command, the “job length score” of each job is equal to their job length (i.e., the number of lines of code in the script) like in Section 1.2.3. • The scheduler will re-assess the ready queue every time slice. For this exercise, we will use a time slice of 1 instruction. o After a given time-slice, the scheduler “ages” all the jobs that are in the ready queue, apart from the current head of the queue. o The aging process decreases a job’s “job length score” by 1. The job length score cannot be lower than 0. o If after the aging procedure there is a job in the queue with a score that is lower than the current running job, the following happens: ▪ The current running job is preempted ▪ The job with the new lowest job length score is placed at the head of the running queue. In case of a tie, the process closer to the head of the running queue has priority. ▪ The scheduler runs the new process in the head of the ready queue. o If after the aging procedure the current head of the ready queue is still the job with the lowest “job length score”, then the current job continues to run for the next time slice. prog1 code prog2 code prog3 code echo helloP1 set x 10 echo $x echo byeP1 echo helloP2 set y 20 echo $y print y echo helloP3 set z 30 echo byeP3echo byeP2 Execution of SJF with aging and a time slice of 1 instruction; the state of the ready queue shown in comments: $ exec prog1 prog2 prog3 AGING helloP3 // (P3, 3), (P1, 4), (P2, 5) → aging (P3, 3), (P1, 3), (P2, 4) → no promotion //Nothing printed for set z 30 // (P3, 3), (P1, 3), (P2, 4) →aging (P3, 3), (P1, 2), (P2, 3) →promote P1 helloP1 // (P1, 2), (P2, 3), (P3, 3) →aging (P1, 2), (P2, 2), (P3, 2) →no promotion //Nothing printed for set x 10 // (P1, 2), (P2, 2), (P3, 2) →aging (P1, 2), (P2, 1), (P3, 1) →promote P2 helloP2 // (P2, 1), (P3, 1), (P1, 2) →aging (P2, 1), (P3, 0), (P1, 1) →promote P3 byeP3 // (P3, 0), (P1, 1), (P2, 1) →aging (P3, 0), (P1, 0), (P2, 0), →promote P1 10 // (P1, 0), (P2, 0), no more aging possible byeP1 // (P1, 0), (P2, 0), no more aging possible //Nothing printed for set y 20 // (P2, 0), no more aging possible 20 // (P2, 0), no more aging possible 20 // (P2, 0), no more aging possible byeP2 // (P2, 0), no more aging possible $ 1.2.5. Multithreaded scheduler So far, the scheduler is single-threaded. In this final exercise you will transform the scheduler from singlethread to multi-threaded. Hint: Part 1 and Part 2 of this exercise are preparation for adding the multi-threading. You do not need to implement multiple threads until Part 3. Part 1. Execution in the background. Before implementing the multi-threaded scheduler, we need to add one more option, i.e., the # option, to the exec command: exec prog1 [prog2 prog3] POLICY # • The semantics of exec are the same as described in 1.2.2. • # is an optional parameter that indicates execution in the background (similar to the & command in the Linux terminal). If exec is run with #, exec will be run in the background and the control in the shell returns immediately to the batch script; the following instruction will be executed normally. • This is achieved by converting the rest of the Shell input into a program and running it, as you are running programs in the exec command. That is, read the rest of the user input as if it were another program prog0, and then schedule it as such. • All the programs, including the Shell code program, are run according to POLICY. Example execution Commands (prog1, prog2, prog3 same as in Section 1.2.2; RR policy is the same as in Section 1.2.3) exec prog1 RR echo progDONE echo progDONE2 echo progDONE3 exec prog1 RR # echo progDONE echo progDONE2 echo progDONE3 Execution helloP1 10 byeP1 progDONE progDONE2 progDONE3 progDONE progDONE2 // batch script has priority helloP1 // Only 1 line printout, as the set command does not have an output progDONE3 10 byeP1Assumptions • Regardless of the scheduling policy, you can assume that the main shell program will be placed at the head of the running queue. • You can assume that only one exec command will be run with the # option in each testcase. • You can assume that the # option will only be used in batch mode. • You can assume that if an exec command with the # option is launched with a POLICY P, then all following exec commands will use the same POLICY P. We will not be testing different policies in the same testcase. Part 2. RR policy with extended time slice. Add a new RR30 policy, where each process gets to run for 30 instructions before it is switched out. The rest of the implementation is identical to the RR policy described in Section 1.2.3. Note that the multithreaded scheduler (Part 3 below) will only be tested with RR and RR30. Part 3. Multithreaded scheduler. To enable multi-threaded scheduling, we will add one more option to the exec command: exec prog1 [prog2 prog3] POLICY [#] MT If MT appears at the end of the exec command, the multi-threaded scheduler is enabled. Once the multithreaded scheduler has been enabled by one of the exec commands, it remains enabled for the entire duration of the testcase (i.e., the threads are terminated only when quit is called). Your multi-threaded scheduler will consist of a pool of two worker threads, created by using the pthreads library. The two worker threads will handle the requests (i.e., programs that are ready to be run in the running queue). Note that, up to this point, your scheduler is single-threaded. Therefore, the programs are executed sequentially, according to the POLICY, and the instructions interleaving is deterministic. With a multi-threaded scheduler that uses two worker threads, two programs can run concurrently, leaving room for non-determinism in the output. • Note that printf locks stdout, so you do not have to worry about output from echo commands in different threads becoming interleaved if echo is implemented with a single call to printf. • If you have problems with output becoming interleaved, consider using a (single) mutex to lock both the print and echo functions, so that only one thread may execute either one at a time. If the # option is used in exec, the remainder of the main program is treated as a separate program and placed at the top of the ready queue. Then, the execution resumes normally, according to the policy. If quit is called and the ready queue is not empty, the quit implementation needs to join with the scheduler threads.For instance, for the following example: pA code pB code pC code echo A echo A echo A echo A echo B echo B echo B echo B echo C echo C echo C echo C Commands (RR policy is the same as in Section 1.2.3) //single-thread scheduler exec pA pB pC RR //multi-thread scheduler exec pA pB pC RR MT Execution (ready queue RQ shown in comments) A // RQ: pA pB pC A // RQ: pA pB pC B // RQ: pB pC pA B // RQ: pB pC pA C // RQ: pC pA pB C // RQ: pC pA pB A // RQ: pA pB pC A // RQ: pA pB pC B // RQ: pB pC pA B // RQ: pB pC pA C // RQ: pC pA pB C // RQ: pC pA pB // RQ: pA pB pC → pA and pB picked up by the 2 workers W1, W2 A // RQ: pC . pC is alone in RQ B B // pB at end of time slice. Is put at the end of RQ. //RQ: pC pB → W2 picks up pC A // pA at end of time slice. Is put at the end of RQ. //RQ: pB pA →W1 picks up pB C B C // pC at end of time slice. Is put at the end of RQ. //RQ: pA pC →W2 picks up pA B // pB done //RQ: pC →W1 picks up pC C A A // pA done C // pC done // the order above is non-deterministic; the deterministic part is // that four As, four Bs, and four Cs are printed. 2. TESTCASES We provide 20 testcases and expected outputs in the starter code repository. Please run the testcases to ensure your code runs as expected, and make sure you get similar results in the automatic tests. IMPORTANT: The grading infrastructure uses batch mode, so make sure your program produces the expected outputs when testcases run in batch mode. You can assume that the grading infrastructure will run one test at a time in batch mode, and that there is a fresh recompilation between two testcases. 3. WHAT TO HAND IN The assignment is due on October 25, 2024 at 23:59, no extensions. Your final grade will be determined by running the code in the GitLab repository that is crawled by our grading infrastructure. We will take into account the most recent commit that happened before the deadline, on the main branch of your fork. In addition to the code, please include a README mentioning the author name(s) and McGill ID(s), any comments the author(s) would like the TA to see, and mention whether the code uses the starter code provided by the OS team or not. The project must compile on the mimi server by running make clean; make myshThe project must run in batch mode, i.e. ./mysh < testfile.txt Feel free to modify the Makefile to add more structure to your code, but make sure that the project compiles and runs using the commands above. (We will use your Makefile.) Note: You must submit your own work. You can speak to each other for help but copied code will be handled as to McGill regulations. Submissions are automatically checked via plagiarism detection tools. 4. HOW IT WILL BE GRADED Your program must compile and run on our grading serverto be graded. If the code does not compile/run using the commands in Section 3, in our grading infrastructure you will receive 0 points for the entire assignment. If you think your code is correct and there is an issue with the grading infrastructure, contact [email protected]. Your assignment is graded out of 20 points. You were provided 20 testcases, with expected outputs. If your code matches the expected output, you will receive 1 point for each testcase. You will receive 0 points for each testcase where your output does not match the expected output. For tests with nondeterministic output, we test that you output the expected number of each token in a feasible order, rather than checking for exact matches. Formatting issues such as tabs instead of spaces, new lines, etc. will not be penalized. The TA will look at your source code only if the program runs (correctly or not). The TA looks at your code to verify that you implemented the requirement as requested. Specifically: • Hardcoded solutions will receive 0 points for the hardcoded testcase, even if the output is correct. • Programming expectations. Your code needs to have a reasonable and consistent programming style. If not, your TA may remove up to 6 points, as they see fit. • You must write this assignment in the C Programming language, otherwise the assignment will receive 0 points.

$25.00 View

[SOLVED] Ecse 324: computer organization lab 3: keyboard and vga

In this lab, you will explore the more complex input/output capabilities of the DE1-SoC: the PS/2 keyboard port, and VGA display. You will begin by writing a driver for each of these devices, and then put them together into an application. Summary of Deliverables ● Source code for: ○ A library implementing a complete VGA driver ○ A library implementing a complete PS/2 driver ○ An application that draws maps on the VGA screen in response to PS/2 input ● Demo, no longer than five (5) minutes (week of April 4th) ● Report, no longer than four (4) pages (10 pt font, 1” margins) (due April 12th at 11:59 pm) Please submit the above in a single .zip archive, using the following file name conventions: ● Code: part1.s, part2.s, part3.s ● Report: StudentID_FullName_Lab1_report.pdf Grading Summary ● 50% Demo ● 50% Report Changelog ● 21-Mar-2022 Initial revision ● 24-Mar-2022 Fixed a typo in Part 3 specifying what keys should be pressed to cycle through flags.Overview In this lab we will use the high level I/O capabilities of the DE1-SoC simulator to 1. display pixels and characters using the VGA controller, and 2. accept keyboard input via the PS/2 port. For each of these topics, we will create a driver. We will test the drivers both individually and in tandem by means of test applications. Part 1: Drawing things with VGA The DE1-SoC computer has a built-in VGA controller that can render pixels, characters or a combination of both. The authoritative resource on these matters is Sections 4.2.1 and 4.2.4 of the DE1-SoC Computer Manual. This section of the lab provides a quick overview that should suffice for the purpose of completing this lab. To render pixels, the VGA controller continuously reads the pixel buffer, a region in memory starting at 0xc8000000 that contains the color value of every pixel on the screen. Colors are encoded as 16-bit integers that reserve 5 bits for the red channel, 6 bits for the green channel and 5 bits for the blue channel. That is, every 16-bit color is encoded like so: 15 … 11 10 … 5 4 … 0 Red Green Blue The pixel buffer is 320 pixels wide and 240 pixels high. Individual pixel colors can be accessed at 0xc8000000 | (y

$25.00 View

[SOLVED] Operating systems comp 310 – ecse 427 assignment #1: building an os shell

1. Infrastructure Description Welcome to the first OS assignment where we will build an OS Shell! This is the first of a series of three assignments that build upon each other. By the end of the semester, you will have a running simulation of a simple Operating System and you will get a good idea of how the basic OS modules work. You will use the C programming language in your implementation, since most of the practical operating systems kernels are written in the C/C++ (e.g., including Windows, Linux, MacOS, and many others). The assignment is presented from a Linux point of view using a server like Mimi. Our grading infrastructure will pull your code from GitLab automatically and will run the unit tests for this assignment every day on the Mimi server. The autograder may not run for the first few days as we take all the administrative steps to set it up. Starting shortly after the team registration deadline, you will receive a daily report stating whether your code passes the unit tests. Please make sure your assignment runs on our server, as this is the reference machine we use for grading. To get your code picked up by our grading infrastructure we will rely on GitLab. It is mandatory to use GitLab for this coursework. For more information about GitLab and the grading infrastructure, please refer to the tutorial posted on MyCourses as part of Lab 1. For local development, and quicker testing turn-around, you can use the SOCS server mimi.cs.mcgill.ca, which you can reach remotely using ssh or putty. You need a SOCS account to access the mimi servers, and you need to either be on campus or connected to the McGill VPN. If you do not have a SOCS account (e.g., you might not have one if you are an ECSE student) please follow the instructions here to obtain one. To get started, fork the following repository, which contains the starter code and the testcases for this assignment. https://gitlab.cs.mcgill.ca/mkopin/comp310-ecse427-coursework-f24 IMPORTANT: If you have already forked your repository before the release date of the assignment, please make sure that your version of the starter code is up-to-date (i.e., sync your fork with the upstream repository mkopin/comp310-ecse427-coursework-f24) before starting to work on the code. 1.1 Starter files description: We provided you with a simple shell to start with, which you will enhance in this assignment. Take a moment to get familiar with the code.Compiling your starter shell • Use the following command to compile: make mysh • Re-compiling your shell after making modifications: make clean; make mysh • Note: The starter code compiles and runs on Mimi and on our server. If you’d like to run the code in your own Linux virtual machine, you may need to install build essentials to be able to compile C code: sudo apt-get install build-essential Your code should be able to compile in any environment with a C compiler so long as you do not hardcode assumptions about the machine into your code. For example, write `sizeof(int)` rather than assuming that this value is 4. Running your starter shell • Interactive mode: From the command line prompt type: ./mysh • Batch mode: You can also use input files to run your shell. To use an input file, from the command line prompt type: ./mysh < testfile.txt Starter shell interface. The starter shell supports the following commands: COMMAND DESCRIPTION help Displays all the commands quit Exits / terminates the shell with “Bye!” set VAR STRING Assigns a value to shell memory print VAR Displays the STRING assigned to VAR run SCRIPT.TXT Executes the file SCRIPT.TXT More details on command behavior: • The commands are case sensitive. • If the user inputs an unsupported command the shell displays “Unknown command”. • set VAR STRING first checks to see if VAR already exists. If it does exist, STRING overwrites the previous value assigned to VAR. If VAR does not exist, then a new entry is added to the shell memory where the variable name is VAR and the contents of the variable is STRING. For now, each value assigned to a variable is a single alphanumeric token (i.e., no special characters, no spaces, etc.). For example: ▪ set x 10 creates a new variable x and assigns to it the string 10. ▪ set name Bob creates a new variable called name with string value Bob. ▪ set x Mary, replaced the value 10 with Mary. • print VAR first checks to see if VAR exists. If it does not exist, then it displays the error “Variable does not exist”. If VAR does exist, then it displaysthe STRING. For example: print x from the above example will display Mary. • run SCRIPT.TXT assumes that a text file exists with the provided file name, in the current directory. It opens that text file and then sends each line one at a time to the interpreter. Theinterpreter treats each line of text as a command. At the end of the script, the file is closed, and the command line prompt is displayed once more. While the script executes the command line prompt is not displayed. If an error occurs while executing the script due a command syntax error, then the error is displayed, and the script continues executing. 1.2 Your tasks: Your task is to add the following functionality to the starter shell. 1.2.1. Enhance the set command. The set command in your starter shell assumes that the STRING variable is a single alphanumeric token. Extend the set command to support values of at most 5 alphanumeric tokens. If the command has more than 5 tokens for the value, the shell will not set the new value and will return the error: “Bad command: Too many tokens”. Assumptions: • You can assume the tokens are separated by a single space. • You can also assume that the length limit for the tokens is < 100 characters each. We will not test with larger values. • Only STRING can have multiple alphanumeric tokens. VAR remains unchanged (i.e., a single alphanumeric token). Example execution (interactive mode): $ set x 10 $ print x 10 $ set x 20 bob alice toto xyz $ print x 20 bob alice toto xyz $ set x 12345 20 bob alice toto xyz Bad command: Too many tokens $ print x 20 bob alice toto xyz 1.2.2. Add the echo command. The echo command is used for displaying strings which are passed as arguments on the command line. This simple version of echo only takes one token string as input. The token can be: • An alphanumeric string. In this case, echo simply displays the string on a new line and then returns the command prompt to the user. Example execution (interactive mode): $ echo mary mary$ • An alphanumeric string preceded by $. In this case, echo checks the shell memory for a variable that has the name of the alphanumeric string following the $ symbol. • If the variable is found, echo displays the value associated to that variable, similar to the print command and then returns the command prompt to the user. • If the variable is not found, echo displays an empty line and then returns the command prompt to the user. Example execution (interactive mode): $ echo $mary // blank line $ $ set mary 123 $ echo $mary 123 $ Assumptions: • You can assume that the token string is

$25.00 View

[SOLVED] Ecse 324: computer organization lab 2: i/o

Part 1: Basic I/O For this part, it is necessary to refer to sections 2.9.1 – 2.9.4 (pp. 7 – 9) and 3.4.1 (p. 14) in the DE1-SoC Computer Manual. Brief overview The hardware setup of the DE1-SoC’s I/O components is fairly straightforward. The system has designated addresses in memory that are connected to hardware circuits on the FPGA through parallel ports, and these hardware circuits, in turn, interface with the physical I/O components. In most cases, the FPGA hardware simply maps the I/O terminals to the memory address designated to it. There are several parallel ports implemented in the FPGA that support input, output, and bidirectional transfers of data between the ARM A9 processor and I/O peripherals. For instance, the state of the slider switches is available to the FPGA on a bus of 10 wires which carry either a logical ’0’ or ’1’. The state of the slider switches is then stored in the memory address reserved for the slider switches (0xFF200040 in this case). It is useful to have slightly more sophisticated FPGA hardware in some cases. For instance, in the case of the push-buttons, in addition to knowing the state of the button, it is also helpful to know whether a falling edge is detected, signaling a keypress. This can be achieved with a simple edge detection circuit in the FPGA. Getting Started: Drivers for slider switches and LEDs Access to the memory addresses designated for I/O interfaces is best facilitated by what are called device drivers. Drivers are subroutines (drivers) that ease the process of reading from and writing to I/O interface addresses, thereby manipulating the state of and data associated with a given peripheral. When writing drivers, it is critical that you follow the subroutine calling conventions presented in this course. 1- Slider Switches: Create a new subroutine labeled read_slider_switches_ASM, which reads the value from the memory location designated for the slider switches data (SW_ADDR) and stores it R0, and then returns. Remember to use the subroutine calling convention, and save processor state (by pushing and popping registers) if needed! If there are fewer than four 32-bit arguments or return values, use registers, rather than the stack, for communication between caller and callee.2- LEDs: Create a new subroutine labeled write_LEDs_ASM. The write_LEDs_ASM subroutine writes the value in R0 to the LEDs memory location (LED_ADDR), and then branches to the address contained in the LR. To help you get started, the code for the slider switches and LEDs drivers have been provided below. Use them as templates for writing future drivers. // Slider Switches Driver // returns the state of slider switches in R0 // post- A1: slide switch state .equ SW_ADDR, 0xFF200040 read_slider_switches_ASM: LDR A2, =SW_ADDR // load the address of slider switch state LDR A1, [A2] // read slider switch state BX LR // LEDs Driver // writes the state of LEDs (On/Off state) in A1 to the LEDs’ memory location // pre– A1: data to write to LED state .equ LED_ADDR, 0xFF200000 write_LEDs_ASM: LDR A2, =LED_ADDR // load the address of the LEDs’ state STR A1, [A2] // update LED state with the contents of A1 BX LR To acquaint yourself with using memory-mapped I/O, write an application that makes use of your read_slider_switches_ASM and the write_LEDs_ASM subroutines to turn on/off the LEDs. Write an infinite loop that calls read_slider_switches_ASM and write_LEDs_ASM in order. Compile and Run your project, and then change the state of the switches in the online simulator to turn on/off the corresponding LEDs. Note that both the Switches and the LEDs panels are located on the top corner of your screen. The figure below demonstrates the result of activating slider switches 0, 4, 5 and 9.More Advanced Drivers: Drivers for HEX displays and push-buttons Now that the basic structure of the drivers has been introduced, we can write more advanced drivers i.e., HEX displays and push-buttons drivers. 1- HEX displays: There are six HEX displays (HEX0 to HEX5) on the DE1-SoC Computer board. You are required to write three subroutines to implement the functions listed below to control the HEX displays. Note! The emulator will complain about word-aligned accesses if you use STRB to write to some HEX displays. This does not indicate an error has occurred. You may turn off Device-specific warnings in the settings, but be careful, as this may hide other warnings worth observing. If you leave this Device-specific warnings enabled, simply continue the execution of your code after the emulator-inserted breakpoint. HEX_clear_ASM: The subroutine will turn off all the segments of the HEX displays passed in the argument. It receives the HEX display indices through register A1 as an argument.HEX_flood_ASM: The subroutine will turn on all the segments of the HEX displays passed in the argument. It receives the HEX display indices through register A1 as an argument. HEX_write_ASM: The subroutine receives HEX display indices and an integer value in 0-15 to display. These are passed in registers A1 and A2, respectively. Based on the second argument (A2), the subroutine will display the corresponding hexadecimal digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F) on the display(s). The subroutines should check the first argument to determine which displays, HEX0-HEX5, have been selected, and write the value of the second argument to each. A loop may be useful here! The HEX display indices can be encoded based on a one-hot encoding scheme, e.g.,: HEX0 = 0x00000001 HEX1 = 0x00000002 HEX2 = 0x00000004 HEX3 = 0x00000008 HEX4 = 0x00000010 HEX5 = 0x00000020 For example, you may pass 0x0000000C to the HEX_flood_ASM subroutine to turn on all the segments of HEX2 and HEX3 displays: mov R0, #0x0000000C BL HEX_flood_ASM 2- Pushbuttons: There are four pushbuttons (PB0 to PB3) on the DE1-SoC Computer board. You are required to write seven subroutines to implement the functions listed below to control the pushbuttons: read_PB_data_ASM: The subroutine returns the indices of the pressed pushbuttons (the keys from the pushbuttons Data register). The indices are encoded based on a one-hot encoding scheme:PB0 = 0x00000001 PB1 = 0x00000002 PB2 = 0x00000004 PB3 = 0x00000008 PB_data_is_pressed_ASM: The subroutine receives a pushbutton index as an argument. Then, it returns 0x00000001 if the corresponding pushbutton is pressed. read_PB_edgecp_ASM: The subroutine returns the indices of the pushbuttons that have been pressed and then released (the edge bits from the pushbuttons’ Edgecapture register). PB_edgecp_is_pressed_ASM: The subroutine receives a pushbutton index as an argument. Then, it returns 0x00000001 if the corresponding pushbutton has been pressed and released. PB_clear_edgecp_ASM: The subroutine clears the pushbutton Edgecapture register. You can read the edgecapture register and write what you just read back to the edgecapture register to clear it. enable_PB_INT_ASM: The subroutine receives pushbutton indices as an argument. Then, it enables the interrupt function for the corresponding pushbuttons by setting the interrupt mask bits to ‘1’. disable_PB_INT_ASM: The subroutine receives pushbutton indices as an argument. Then, it disables the interrupt function for the corresponding pushbuttons by setting the interrupt mask bits to ‘0’. Part 1 Deliverable: Write an application that uses the appropriate drivers created so far to perform the following functions. ● As before, the state of the slider switches will be mapped directly to the LEDs. ● Additionally, the state of the last four slider switches SW3-SW0 (SW3 corresponds to the most significant bit) will be used to set the value of a number from 0-15. This number will be displayed on a HEX display when the corresponding pushbutton is pressed. For example, pressing PB0 will result in the number being displayed on HEX0. When the pushbutton is released, the value displayed on the HEX display should remain unchanged, even if you change the state of the slider switches. Since there are no pushbuttons tocorrespond to HEX4 and HEX5, you must turn on all the segments of the HEX4 and HEX5 displays. ● Finally, asserting slider switch SW9 should clear all the HEX displays. Figure below demonstrates the result of activating slider switches 0 and 3 and pressing pushbutton 0 (PB0). Remember, you have to release the pushbuttons to see the results as the Edgecapture register is updated once the pushbuttons are released (unchecked).Part 2: Timers For this part, it is necessary to refer to sections 2.4.1 (p. 3) and 3.1 (p. 14) in the DE1-SoC Computer Manual. Brief overview Timers are simply hardware counters that are used to measure time and/or synchronize events. They run on a known clock frequency that is programmable in some cases. Timers are usually (but not always) down counters, and by programming the start value, the time-out event (when the counter reaches zero) occurs at fixed time intervals. ARM A9 Private Timer drivers There is one ARM A9 private timer available on the DE1-SoC board. The timer uses a clock frequency of 200 MHz. You need to configure the timer before using it. To configure the timer, you need to pass three arguments to the “configuration subroutine”. The arguments are: 1- Load value: The ARM A9 private timer is a down counter and requires an initial count value. Use A1 to pass this argument. 2- Configuration bits: Use A2 to pass this argument. Read sections 2.4.1 (p. 3) and 3.1 (p. 14) in the De1-SoC Computer Manual carefully to learn how to handle the configuration bits. The configuration bits are stored in the Control register of the timer. You are required to write three subroutines to implement the functions listed below to control the timers: ARM_TIM_config_ASM: The subroutine is used to configure the timer. Use the arguments discussed above to configure the timer. ARM_TIM_read_INT_ASM: The subroutine returns the “F” value (0x00000000 or 0x00000001) from the ARM A9 private timer Interrupt status register. ARM_TIM_clear_INT_ASM: The subroutine clears the “F” value in the ARM A9 private timer Interrupt status register. The F bit can be cleared to 0 by writing a 0x00000001 into the Interrupt status register.To test the functionality of your subroutines, write assembly that uses the ARM A9 private timer. Use the timer to count from 0 to 15 and show the count value on the HEX display (HEX0). You must increase the count value by 1 every time the “F” value is asserted (“F” becomes ‘1’). The count value must be reset when it reaches 15 (1, 2, 3, …, E, F, 0, 1, …). The counter should be able to count in increments of 1 second. Remember, you must clear the timer interrupt status register each time the timer sets the “F” bit in the interrupt status register to 1 by calling the ARM_TIM_clear_INT_ASM subroutine. Creating an application: Polling-based stopwatch Part 2 Deliverable: Create a simple stopwatch using the ARM A9 private timer, pushbuttons, and HEX displays. The stopwatch should be able to count in increments of 100 milliseconds. Use the ARM A9 private timer to count time. Display milliseconds on HEX0, seconds on HEX2-1, minutes on HEX3-4, and hours on HEX5. PB0, PB1, and PB2 will be used to start, stop and reset the stopwatch, respectively. Use an endless loop to poll the pushbutton edgecapture register and the “F” bit from the ARM A9 private timer interrupt status register. Note! The emulator is not real-time capable (1 second of simulation of your stopwatch may take much more than 1 second of wall-clock time). You will not be able to compare the passage of wall-clock time with your stopwatch’s behavior to determine whether or not it is functioning correctly.Part 3: Interrupts For this part, it is necessary to refer to section 3 (pp. 13-17) in the DE1-SoC Computer Manual. Furthermore, detailed information about the interrupt drivers is provided in the “Using the ARM Generic Interrupt Controller” document available here. Brief Overview Interrupts are hardware or software signals that are sent to the processor to indicate that an event has occurred that needs immediate attention. When the processor receives an interrupt, it pauses the code currently executing, handles the interrupt by executing code defined in an Interrupt Service Routine (ISR), and then resumes normal execution. Apart from ensuring that high priority events are given immediate attention, interrupts also help the processor to utilize resources more efficiently. Consider the polling application from the previous section, where the processor periodically checked the pushbuttons for a keypress event. Asynchronous events such as this, if assigned an interrupt, can free the processor to do other work between events. ARM Generic Interrupt Controller The ARM generic interrupt controller (GIC) is a part of the ARM A9 MPCORE processor. The GIC is connected to the IRQ interrupt signals of all I/O peripheral devices that are capable of generating interrupts. Most of these devices are normally external to the A9 MPCORE, and some are internal peripherals (such as timers). The GIC included with the A9 MPCORE processor in the Altera Cyclone V SoC family handles up to 255 interrupt sources. When a peripheral device sends its IRQ signal to the GIC, then the GIC can forward a corresponding IRQ signal to one or both of the A9 cores. Software code that is running on the A9 core can then query the GIC to determine which peripheral device caused the interrupt, and take appropriate action. The ARM Cortex-A9 has several main modes of operation and the operating mode of the processor is indicated in the current processor status register CPSR. In this Lab, we only use IRQ mode. A Cortex-A9 processor enters IRQ mode in response to receiving an IRQ signal from the GIC. Before such interrupts can be used, software code has to perform a number of steps:1. Ensure that IRQ interrupts are disabled in the A9 processor by setting the IRQ disable bit in the CPSR to 1. 2. Configure the GIC. Interrupts for each I/O peripheral device that is connected to the GIC are identified by a unique interrupt ID. 3. Configure each I/O peripheral device so that it can send IRQ interrupt requests to the GIC. 4. Enable IRQ interrupts in the A9 processor, by setting the IRQ disable bit in the CPSR to 0. An example assembly program that demonstrates use of interrupts follows. The program responds to interrupts from the pushbutton KEY port in the FPGA. The interrupt service routine for the pushbutton KEYs indicates which KEY has been pressed on the HEX0 display. You can use this code as a template when using interrupts in the ARM Cortex-A9 processor. First, you need to add the following lines at the beginning of your assembly code to initialize the exception vector table. Within the table, one word is allocated to each of the various exception types. This word contains branch instructions to the address of the relevant exception handlers. .section .vectors, “ax” B _start // reset vector B SERVICE_UND // undefined instruction vector B SERVICE_SVC // software interrupt vector B SERVICE_ABT_INST // aborted prefetch vector B SERVICE_ABT_DATA // aborted data vector .word 0 // unused vector B SERVICE_IRQ // IRQ interrupt vector B SERVICE_FIQ // FIQ interrupt vector Then, add the following to configure the interrupt routine. Note that the processor’s modes have their own stack pointers and link registers (see Figure 3 in “Using the ARM Generic Interrupt Controller”). At a minimum, you must assign initial values to the stack pointers of any execution modes that are used by your application. In our case, when an interrupt occurs, the processor enters IRQ mode. Therefore, we must assign an initial value to the IRQ mode stack pointer. Usually, interrupts are expected to be executed as fast as possible. As a result, on-chip memories are used in IRQ mode.The following code illustrates how to set the stack to the A9 on-chip memory in IRQ mode: .text .global _start _start: /* Set up stack pointers for IRQ and SVC processor modes */ MOV R1, #0b11010010 // interrupts masked, MODE = IRQ MSR CPSR_c, R1 // change to IRQ mode LDR SP, =0xFFFFFFFF – 3 // set IRQ stack to A9 onchip memory /* Change to SVC (supervisor) mode with interrupts disabled */ MOV R1, #0b11010011 // interrupts masked, MODE = SVC MSR CPSR, R1 // change to supervisor mode LDR SP, =0x3FFFFFFF – 3 // set SVC stack to top of DDR3 memory BL CONFIG_GIC // configure the ARM GIC // To DO: write to the pushbutton KEY interrupt mask register // Or, you can call enable_PB_INT_ASM subroutine from previous task // to enable interrupt for ARM A9 private timer, use ARM_TIM_config_ASM subroutine LDR R0, =0xFF200050 // pushbutton KEY base address MOV R1, #0xF // set interrupt mask bits STR R1, [R0, #0x8] // interrupt mask register (base + 8) // enable IRQ interrupts in the processor MOV R0, #0b01010011 // IRQ unmasked, MODE = SVC MSR CPSR_c, R0 IDLE: B IDLE // This is where you write your objective taskThen, you need to define the exception service routines using the following: /*— Undefined instructions ————————————–*/ SERVICE_UND: B SERVICE_UND /*— Software interrupts —————————————-*/ SERVICE_SVC: B SERVICE_SVC /*— Aborted data reads ——————————————*/ SERVICE_ABT_DATA: B SERVICE_ABT_DATA /*— Aborted instruction fetch ———————————–*/ SERVICE_ABT_INST: B SERVICE_ABT_INST /*— IRQ ———————————————————*/ SERVICE_IRQ: PUSH {R0-R7, LR} /* Read the ICCIAR from the CPU Interface */ LDR R4, =0xFFFEC100 LDR R5, [R4, #0x0C] // read from ICCIAR /* To Do: Check which interrupt has occurred (check interrupt IDs) Then call the corresponding ISR If the ID is not recognized, branch to UNEXPECTED See the assembly example provided in the De1-SoC Computer_Manual on page 46 */ Pushbutton_check: CMP R5, #73 UNEXPECTED: BNE UNEXPECTED // if not recognized, stop here BL KEY_ISR EXIT_IRQ: /* Write to the End of Interrupt Register (ICCEOIR) */ STR R5, [R4, #0x10] // write to ICCEOIR POP {R0-R7, LR} SUBS PC, LR, #4 /*— FIQ ———————————————————*/ SERVICE_FIQ: B SERVICE_FIQThen you are required to add the following to configure the Generic Interrupt Controller (GIC): CONFIG_GIC: PUSH {LR} /* To configure the FPGA KEYS interrupt (ID 73): * 1. set the target to cpu0 in the ICDIPTRn register * 2. enable the interrupt in the ICDISERn register */ /* CONFIG_INTERRUPT (int_ID (R0), CPU_target (R1)); */ /* To Do: you can configure different interrupts by passing their IDs to R0 and repeating the next 3 lines */ MOV R0, #73 // KEY port (Interrupt ID = 73) MOV R1, #1 // this field is a bit-mask; bit 0 targets cpu0 BL CONFIG_INTERRUPT /* configure the GIC CPU Interface */ LDR R0, =0xFFFEC100 // base address of CPU Interface /* Set Interrupt Priority Mask Register (ICCPMR) */ LDR R1, =0xFFFF // enable interrupts of all priorities levels STR R1, [R0, #0x04] /* Set the enable bit in the CPU Interface Control Register (ICCICR). * This allows interrupts to be forwarded to the CPU(s) */ MOV R1, #1 STR R1, [R0] /* Set the enable bit in the Distributor Control Register (ICDDCR). * This enables forwarding of interrupts to the CPU Interface(s) */ LDR R0, =0xFFFED000 STR R1, [R0] POP {PC} /* * Configure registers in the GIC for an individual Interrupt ID * We configure only the Interrupt Set Enable Registers (ICDISERn) and * Interrupt Processor Target Registers (ICDIPTRn). The default (reset) * values are used for other registers in the GIC* Arguments: R0 = Interrupt ID, N * R1 = CPU target */ CONFIG_INTERRUPT: PUSH {R4-R5, LR} /* Configure Interrupt Set-Enable Registers (ICDISERn). * reg_offset = (integer_div(N / 32) * 4 * value = 1

$25.00 View

[SOLVED] Ecse 324: computer organization lab 1: basic assembly language programming

Part 1: Calling all functions! In this part, your task is to implement a number of different relatively straightforward functions. Assume that main in the C code below begins at _start in your assembly program. All programs should terminate with an infinite loop, as in exercise (5) of Lab 0. Subroutine calling convention It is important to carefully respect subroutine calling conventions in order to prevent call stack corruption. The convention which we will use for calling a subroutine in ARM assembly is as follows. The caller (the code that is calling a function) must: ● Move arguments into R0 through R3. (If more than four arguments are required, the caller should PUSH the arguments onto the stack.) ● Call the subroutine func using BL func. The callee (the code in the function that is called) must: ● Move the return value into R0. ● Ensure that the state of the processor is restored to what it was before the subroutine call by POPing arguments off of the stack. ● Use BX LR to return to the calling code. Note that the state of the processor can be saved and restored by pushing R4 through LR onto the stack at the beginning of the subroutine and popping R4 through LR off the stack at the end of the subroutine. For an example of how to perform a function call in assembly consider the implementation of vector dot product. Note that this code is an expansion of the example presented in 2-isa.pdf. .global _start // define entry point // initialize memory n: .word 6 // the length of our vectors vecA: .word 5,3,-6,19,8,12 // initialization for vector A vecB: .word 2,14,-3,2,-5,36 // initialization for vector B vecC: .word 19,-1,-37,-26,35,4 // initialization for vector C result: .space 8 // uninitialized space for the results_start: // execution begins here! LDR A1, =vecA // put the address of A in A1 LDR A2, =vecB // put the address of B in A2 LDR A3, n // put n in A3 BL dotp // call dotp function LDR V1, =result // put the address of result in V1 STR A1, [V1] // put the answer (0x1f4, #500) in result LDR A1, =vecA // put the address of A in A1 LDR A2, =vecC // put the address of C in A2 LDR A3, n // put n in A3 BL dotp // call dotp function STR A1, [V1, #4] // put the answer (0x94, #148) in result+4 stop: B stop // calculate the dot product of two vectors // pre– A1: address of vector a // A2: address of vector b // A3: length of vectors // post- A1: result dotp: PUSH {V1-V3} // push any Vn that we use MOV V1, #0 // V1 will accumulate the product dotpLoop: LDR V2, [A1], #4 // get vectorA[i] and post-increment LDR V3, [A2], #4 // get vectorB[i] and post-increment MLA V1, V2, V3, V1 // V1 += V2*V3 SUBS A3, A3, #1 // i– and set condition flags BGT dotpLoop MOV A1, V1 // put our result in A1 to return it POP {V1-V3} // pop any Vn that we pushed BX LR // return Questions? There’s a channel for that in Teams.Exponential function You’ll start with a function that calculates xn using a loop as follows in the C code below. Note that you need not explicitly implement a function called main. int exp(int x, int n) { int result = 1; for (int i=0; i> 1); else // n is even return exp(x * x, n >> 1); } // exp int main(int argc, char *argv[]) { int a, b; a = exp(2, 10); // 1024 b = exp(-5, 5); // -3125 } // mainNote! Your implementation of exp must match the function prototype above: use R0 to pass x; use R1 to pass n. Return the result in R0. Exercises 1. Write an assembly program (part1-exp.s) that computes xn for any integer x and any positive integer n. exp must be implemented as a function that is called by your program more than once. 2. Write an assembly program (part1-fact.s) that uses recursive function calls to calculate n!. fact must be implemented as a function that is called by your program more than once. 3. Write an assembly program (part1-expbysq.s) that uses recursive function calls to compute xn for any integer x and any positive integer n using exponentiation by squaring. exp must be implemented as a function that is called by your program more than once. Part 2: Sort this out quickly In this part, your task is to implement the quicksort algorithm. Quicksort is a recursive algorithm; you will additionally implement a helper function that swaps elements in an array. /* swap two elements in the array */ void swap(int *array, int a, int b) { int temp; temp = array[a]; array[a] = array[b]; array[b] = temp; } // swap /* main quicksort algorithm */ void quicksort(int *array, int start, int end) { int i, j, pivot, temp; if (start < end) { pivot = start; i = start; j = end; while (i < j) { // move i right until we find a number greater than the pivot while (array[i] array[pivot]) j–; // swap the elements at these positions // unless they are already relatively sorted if (i < j) swap(array, i, j); } // while // swap pivot and element j swap(array, pivot, j); // recurse on the subarrays before and after element j quicksort(array, start, j-1); quicksort(array, j+1, end); } // if } // quicksort /* program initializes an array and calls quicksort */ int main(int argc, char* argv[]) { // initialization int length = 10; int numbers[10] = {68, -22, -31, 75, -10, -61, 39, 92, 94, -55}; int *ptr = &numbers[0]; quicksort(ptr, 0, length-1); // output: numbers = {-61, -55, -31, -22, -10, 39, 68, 75, 92, 94} } // main Note! Your implementation of quicksort must match the function prototype above: use R0 to pass a pointer to an array; use R1 to pass the lowest index to be sorted; use R2 to pass the highest index to be sorted. Exercise 1. Write an assembly program (part2.s) that implements the quicksort algorithm to sort the given array in ascending order. quicksort must be implemented as a function that is called by your program.Deliverables Your demo is limited to 5 minutes. It is useful to highlight that your software computes correct partial and final answers; draw our attention to the registers and memory contents at appropriate points to demonstrate that your software operates as expected. Your demo will be graded by assessing, for each algorithm, the correctness of the observed behavior, and the correctness of your description of that behavior. In your report, for each algorithm, describe: ● Your approach (e.g., how you used subroutines, including passing arguments and returning results, how you used the stack, etc) ● Challenges you faced, if any, and your solutions ● Shortcomings, possible improvements, etc Your report is limited to four pages, total (no smaller than 10 pt font, no narrower than 1” margins, no cover page). It will be graded by assessing, for each algorithm, your report’s clarity, organization, and technical content. Grading Your demo and report are equally weighted. The breakdown for the demo and report are as follows: Demo ● 10% Part 1A: Exponential function ● 20% Part 1B: Factorial function ● 30% Part 1C: Exponentiation by squares ● 40% Part 2: Quicksort Each section will be graded for (a) clarity, (b) technical content, and (c) correct execution: ● 1pt clarity: the demo is clear and easy to follow ● 1pt technical content: correct terms are used to describe your software ● 3pt correctness: given an input, the correct output is clearly demonstrated Report ● 10% Part 1A: Exponential function ● 20% Part 1B: Factorial function ● 30% Part 1C: Exponentiation by squares ● 40% Part 2: QuicksortEach section will be graded for: (a) clarity, (b) organization, and (c) technical content: ● 1pt clarity: grammar, syntax, word choice ● 1pt organization: clear narrative flow from problem description, approach, testing, challenges, etc. ● 3pt technical content: appropriate use of terms, description of proposed approach, description of testing and results, etc. Submission Please submit, on MyCourses, your source code, and report, in a single .zip archive, using the following file name conventions: ● Code: part1-exp.s, part1-fact.s, part1-expbysq.s, part2.s ● Report: StudentID_FullName_Lab1_report.pdf

$25.00 View

[SOLVED] Assignment 3 cse538

Overview– **Part I. Autoregressive and Auto-Encoding Transformer LM (40 Points)** – 1.1 Find the zero-shot accuracy of distil-gpt2 on boolQ (10 points) – 1.2 Finetune the distil-gpt2 LM on boolQ language (10 points) – 1.3 Compute the zero-shot accuracy on boolQ language (5 points) – 1.4 Task fine-tune distilRoberta for boolQ and compute accuracy (15 points) – **Part II. Deeper into Transformer LM with Attention Mods (40 Points)** – 2.1 Modify the transformer distilRoberta (15 points) – 2.2 Task fine-tune distilRoberta & variants for classification and compute accuracy (10 points) – 2.3 Task fine-tune distilRoberta & variants for regression and compute accuracy (15 points) – 2.4 Extra Credit: Improve the architecture (up to 10 points) – **Part III. Application: Context-based Response Generation (20 Points)** – 3.1 Context retrieval using Sentence Transformer (10 points) – 3.2 Generate answers based on retrieved context (10 points)#### Objectives: – Work with pre-trained transformer LMs – Fine-tune LMs – Language Model fine-tuning – Task fine-tuning – Familiarize oneself with coding self-attention by altering it – Use an LM to retrieve context and generate content—## Code StructureIn previous assignments, the function signatures specified under a task were provided as guidance. However, for this assignment, you are free to structure the code as you see fit. Please ensure that you clearly mark the sections and checkpoints. The files linked below provide an outline/template for your reference, but it’s not required to follow it.– Part I: [`a3_p1_lastname_id.py`](https://drive.google.com/file/d/19B39DvtPTcHJziPjVVH5SeH6if4R7Gk-/view) – Part II: [`a3_p2_lastname_id.py`](https://drive.google.com/file/d/1ePsJEoqhVCwoqJgJ9vDb8MarsdBTLViV/view)—## DatasetIn this assignment, you will use three datasets, all available via HuggingFace’s [datasets library](https://huggingface.co/docs/hub/en/datasets-usage), covering task domains of question answering and content classification:### Part 1: [BoolQ](https://huggingface.co/datasets/google/boolq) – **Description:** A true/false (i.e., “boolean”) question answering dataset: “These questions are naturally occurring — they are generated in unprompted and unconstrained settings. Each example is a triplet of (question, passage, answer), with the title of the page as optional additional context.” For each triple, answering a question is the task for this dataset. The passage describes the subject of the question and often contains the answer written in a different way than the question. – Answers come as “true” and “false” but should be mapped to simply “yes” and “no” to make them more aligned with common natural language answers.### Part 2: [Stanford Sentiment Treebank](https://huggingface.co/datasets/stanfordnlp/sst) (v1) – **Description:** A compilation of sentences expressing opinions about a film, split across training, validation, and test sets. Each example consists of (sentence, label, tokens, tree), where the sentence is in plain-text and the label provides the sentiment value for the degree of “positivity” from 0.0 to 1.0. The dataset also provides a parse tree for each sentence with labels for each component, enabling the study of compositional effects of sentiment in language.### Part 3: [Stanford Question Answering Dataset](https://huggingface.co/datasets/rajpurkar/squad) (v1.1) – **Description:** SQuAD offers a collection of question-answer pairs in the context of a Wikipedia article. It is a reading comprehension dataset. Each example is a tuple of (id, title, context, question, answers). Every question has an answer in this version, in the form of a dictionary containing the answer text and the starting index location in the context passage.“`python from datasets import load_dataset# Parts I and II boolq_dataset = load_dataset(‘google/boolq’)# Part II sst_dataset = load_dataset(‘stanfordnlp/sst’)# Part III squad = load_dataset(“squad”) “`## Requirements– You must use **Python version 3.8** or later with **PyTorch 2.1.1** or later and **Numpy 1.22** or later. – We will test your code. – You may do your development in any environment, but runtimes listed will be based on a **2 vCPU system with 1GB memory** and average hard disk drive (an [e2-micro](https://gcloud-compute.com/e2-micro.html) machine available in the free tier for students on GCP).—## Python LibrariesAcceptable machine learning, or statistics libraries are listed below (see version numbers for torch and numpy above). Other data science, machine learning, or statistics related libraries are prohibited —- **ask if unsure**. The intention is for you to implement the algorithms we have gone over and problem-solve in order to best understand the concepts of this course and their practical application. **You may not use any pre-existing implementations of such algorithms even if they are provided in the following libraries.**“`python import os, sys, random, re, collections, string import numpy as np import torch import math import csv import sklearn.model_selection import sklearn.metrics import heapq import matplotlib import tqdm import transformers import datasets import sentence_transformers “`You may now use any AutoModel for getting the LMs. You are welcome to reference any examples from Hugging Face themselves.—## Code-Assistance PolicySee syllabus for code-assistance policy.**Copying code from another student (current or previous), person, or any non-open source is prohibited.** This will result in at least a zero on the assignment and a report to the graduate program director with possibility for more consequences.**Students are equally responsible for making sure their code is not accessible to any other student** as they are for not copying another’s code. Please see the syllabus for additional policies. A word to the wise: Multiple subparts of this assignment have never been given before. Take the time to figure them out on your own, seeking only conceptual help (or talking to TAs/instructor at office hours). This is not only less selfish to your classmates, but it will take you further in life.—## Part I. Autoregressive and Auto-Encoding Transformer LM (40 Points)Your objective is to build an autoregressive and an autoencoder transformer-based LM.– **Filename:** `a3_p1__.py` – **Input:** You should input the dataset via Python imports as described in the introduction. + Your code should run without any arguments as follows: `python a3_p1_lastname_id.py` – **Output:** Place checkpoint output into: `a3_p1___OUTPUT.pdf` + You may copy-paste the loss curve plot### 1.1 Find the zero-shot accuracy of distil-gpt2 on boolQPrompt (i.e., give a history) [distilgpt2](https://huggingface.co/distilbert/distilgpt2) to answer the boolQ validation set questions given their passage. To do this, you will need to load the distilgpt2 pretrained model using the HuggingFace `transformers` library.You will pass the passage followed by the question: `”PASSAGE. QUESTION? ”` where ` ` is the newline token, and request the probability for the next token being “yes” or “no”. Record the answer in a list to be able to evaluate against the labels.Compute overall accuracy, overall macro F1, as well as class-specific precision, recall, and F1 scores (i.e., 8 total scores). You may use existing methods from the allowed libraries to compute these metrics.Note: for this, you will not use the training data at all – that is why it is called “Zero-Shot”.#### Checkpoint 1.1 Print the zero-shot overall accuracy and F1 scores along with the class-specific precision, recall, and F1:“` Overall: acc: .xxx, f1: .xxx Yes: prec: .xxx, rec: .xxx, f1: .xxx No: prec: .xxx, rec: .xxx, f1: .xxx “`Decent results benchmark: overall acc >= 40%, F1 > 0.35 (intended as help to make sure things are working as they should)### 1.2 Finetune the distil-gpt2 LM on boolQ languageFine-tune the distilgpt2 LM for the task of boolean question answering (fine-tuning the language model itself for content of a given task is known as **”instruction tuning”**).To do this, you will load the distilgpt2 model and then continue to train the LM itself where you send each row from the boolQ training set in the same format of passage followed by question, but this time also send the answer (“yes” or “no”) following the question: `”PASSAGE. QUESTION? ”` (either “yes” or “no” without the )Use the [AdamW](https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html) optimizer, which should train a bit faster than SGD, and make sure to pay attention to the loss curve to ensure you have an appropriate initial learning rate, number of epochs, and weight_decay.#### Checkpoint 1.2 Generate a plot of the loss curves during LM fine-tuning and attach it in the PDF.### 1.3 Compute the zero-shot accuracy on boolQ language Run your updated (i.e., instruction tuned) distil-gpt2 for generating answers to the boolQ validation set. Compute overall accuracy, overall macro F1, as well as class-specific precision, recall, and F1 scores (i.e., 8 total scores).#### Checkpoint 1.3 Print the computed metrics as follows:“` Overall: acc: .xxx, f1: .xxx Yes: prec: .xxx, rec: .xxx, f1: .xxx No: prec: .xxx, rec: .xxx, f1: .xxx “`Decent results benchmark: greater than before instruction tuning (e.g., overall acc >= 55%, F1 > 0.4; intended as help to make sure things are working as they should)### 1.4 Task fine-tune distilRoberta for boolQ and compute accuracy Implement task fine-tuning of [distilRoberta](https://huggingface.co/distilbert/distilroberta-base) for doing boolean classification over the boolQ training set.You will alter the architecture such that the final layer is a binary classification layer that outputs probabilities for answering yes and no when given input in the same format as 2.1 (containing the passage followed by the question but no answer: `”PASSAGE. QUESTION? ”`).The model should use binary cross-entropy loss (also known as log loss) with the [AdamW](https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html) optimizer.#### Checkpoint 1.4 – Generate a plot of the loss curve during LM fine-tuning and attach it in the PDF. – Print the overall accuracy and F1 scores along with the class-specific precision, recall, and F1 over the validation set:“` Overall: acc: .xxx, f1: .xxx Yes: prec: .xxx, rec: .xxx, f1: .xxx No: prec: .xxx, rec: .xxx, f1: .xxx “`Decent results benchmark: overall acc >= 65%, F1 > 0.5 (intended as help to make sure things are working as they should)*Code should complete within 12 minutes with an NVidia T4 GPU.*—## Part II. Deeper into Transformer LM with Attention Mods (40 Points)Your objective is to task fine-tune an auto-encoding LM for the two dataset tasks as well as experiment with alterations to some attention layers.– **Filename:** `a3_p2__.py` – **Input:** You should input the dataset via Python imports as described in the introduction. + Your code should run without any arguments as follows: `python a3_p2_lastname_id.py` – **Output:** Place checkpoint output into: `a3_p2___OUTPUT.pdf`. (You may copy-paste the loss curve plot into PDF)### 2.1 Modify the transformer distilRobertaMake two distinct new versions of distilroberta by altering the last 2 transformer encoder layers of distilRoberta LM (2.1.2 and 2.1.3) as well as a baseline that makes all weights random (2.1.1) as follows:– 2.1.1 distilRB-rand: Reset all of the learned parameters in the entire transformer LM to be drawn from a random gaussian for any linear layer weights and 1s for any norm weights. This model should perform much worse. It will function as a baseline to compare the benefits of LM pretraining before task fine-tuning.– 2.1.2 distilRB-KQV: Alter self-attention such that it only has a single set of weights for the query, key, and value. Initialize the shared KQV linear layer weights to be the mean of the pretrained key and query weights.– 2.1.3 distilRB-nores: Alter the transformer such that it has no residual link, removing the “add” residuals step but keeping the “norm” (see red “X”s in the figure of the standard transformer encoder). Thus, it has no summation with the previous layer’s output, but it should still maintain the normalization steps. Apply these changes independently on new model instances (only one at a time: so distilRB-KQV is built from distilroberta directly and not from distilRB-rand, and distilRB-nores is built from distilroberta with its weights and not from distilRB-KQV). This way, you can test the effect each has on the accuracy of the model with only the one change that each makes.We will use the modified LMs for classification and regression tasks, so make sure to change the output layer accordingly.### 2.2 Task fine-tune distilRoberta & variants for classification (boolQ) and compute accuracy Similar to Part 1.4, task fine-tune the variants – distilroberta, distilRB-KQV, and distilRB-nores for binary classification on boolQ training set. Do not task fine-tune distilRB-rand since we’re using it as a baseline. distilrobera results will be the same as 1.4.#### Checkpoint 2.2 Print the overall accuracy and F1 scores over the boolQ validation set.“` boolq validation set: distilRB-rand: overall acc: .xxx, f1: .xxx distilroberta: overall acc: .xxx, f1: .xxx distilRB-KQV: overall acc: .xxx, f1: .xxx distilRB-nores: overall acc: .xxx, f1: .xxx “`### 2.3 Task fine-tune distilRoberta & variants for regression (SST) and compute accuracy Implement task fine-tuning of distilroberta, distilRB-KQV, and distilRB-nores for regression task using the Stanford Sentiment Treebank (SST) training set.You will alter the architecture such that the final layer is a linear regression layer down to a single output when given a single text input (i.e., the text to be labeled with degree of positivity sentiment).The model should use Mean Squared Error (MSE) for the loss function with the [AdamW](https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html) optimizer.#### Checkpoint 2.3 – For distilRoberta: generate the training set loss curve. Also, print the mean absolute error (mae) along with Pearson correlation (r) over the validation set and test set: “` Validation: mae: .xxx, r: .xxx Test: mae: .xxx, r: .xxx “` Note: You may use existing methods from the allowed libraries to compute these metrics. Decent results benchmark: test r > 0.7 (intended as help to make sure things are working as they should) – For distilRB-rand, distilRB-KQV, distilRB-nores: Print the mae and r. “` SST test set: distilRB-rand: mae: .xxx, r: .xxx distilRB-KQV: mae: .xxx, r: .xxx distilRB-nores: mae: .xxx, r: .xxx “`### Extra Credit: Improve the architecture Create a 4th architecture that attempts to improve over distilroberta and fine-tune it for the two tasks’ training sets.#### Checkpoint “extra credit” Print the overall accuracies for the boolQ (validation) and SST (test) sets for both this new model and the best model from 2.2 and 2.3.*Code should complete within 12 minutes with an NVidia T4 GPU.*—## Part III: Application: Context-based Response Generation (20 Points)In this part, you will create a simplified retrieval-based answer generation method. The goal is to showcase one of the many powerful tools available and encourage you to explore further in the class project and future work.– **Filename:** `a3_p3__.py` – **Input:** You should input the dataset via Python imports as described in the introduction. + Your code should run **without any arguments** as follows: `python a3_p3_lastname_id.py` – **Output:** Place checkpoint output into: `a3_p3___OUTPUT.txt`. (You may copy-paste the loss curve plot)### 3.1 Context retrieval using Sentence Transformer Start by loading the Stanford Question Answering Dataset (SQuAD) and the [Sentence Transformer](https://huggingface.co/sentence-transformers) from HuggingFace using the code given below. We will use the validation set since this task does not involve fine-tuning.While the dataset has multiple questions sharing the same context passage, we pick the first 500 examples with unique contexts. We’ll refer to these 500 examples as val500.“`python def get_unique_ctx_examples(squad, n=500): context2idx = {} for i, entry in enumerate(squad[‘validation’]): if not entry[‘context’] in context2idx: context2idx[entry[‘context’]] = [] context2idx[entry[‘context’]].append(i)queries, contexts, answers = [], [], [] for k,v in context2idx.items(): idx = v[0] queries.append(squad[‘validation’][idx][‘question’]) contexts.append(squad[‘validation’][idx][‘context’]) answers.append(squad[‘validation’][idx][‘answers’]) if len(queries) == n: break return queries, contexts, answerssquad = load_dataset(“squad”) model = SentenceTransformer(“all-MiniLM-L6-v2”) queries, contexts, answers = get_unique_ctx_examples(squad) “`Transform the list of context sentences into vector embeddings using the `encode` method of the Sentence Transformer. This should create a 384-dimensional vector for each context sentence.Fill in the code for `retrieve`, which accepts the list of contexts, vector embeddings, and a single string query. The method: – Encodes the query using Sentence Transformer. – Computes the cosine similarity between the query embedding and all val500 context embeddings. – Returns the index and context with highest cosine similarity match with the query.“`python def retrieve(contexts, embeddings, query): # inputs # contexts: list of val500 context strings # embeddings: list/array/tensor of vector embedding of each context string # query: a single question as a string # outputs # idx: index of context vector with highest cosine similarity with query # ret_context: retrieved context#return idx, ret_context “`#### Checkpoint 3.1 For each question in val500, use `retrieve` to fetch the index and context based on cosine similarity. Track a count of correctly retrieved contexts.Print the retrieval accuracy as: #correctly_retrieved / #questions.### 3.2 Generate answers based on retrieved contextSince we can fetch information given a question, the next task is to present it in a conversational manner. Load the [Phi-3-Mini-4K-Instruct](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct) (model id: microsoft/Phi-3-mini-4k-instruct). Load it in `torch.bfloat16` precision.Note: you can also use [Llama-3.2-3B-Instruct](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct) (model id: meta-llama/Llama-3.2-3B-Instruct) as the rest of the code remains the same for both. (Note: this is a gated model and requires the user to fill out an access request form, but generally access will be granted within a few minutes/hours)Fill in the function `generate_response`. It accepts – an LM instance, a query string, and the context obtained from `retrieve`.Create an instruction template for the LM model, generate a response, and return it. You may use the given template or create your own!“`python # Instruction template messages = [ {“role”: “system”, “content”: “You are a helpful AI assistant. ” “Provide one Answer ONLY to the following query based on the context provided below. ” “Do not generate or answer any other questions. ” “Do not make up or infer any information that is not directly stated in the context. ” “Provide a concise answer.” f” Context: {retrieved_text}”}, {“role”: “user”, “content”: query} ] “`“`python def generate_response(model, query, ret_context): # input # model: an instance of LM # query: the question as a string # ret_context: context retrieved from the embedded vectors # output # response: a string of tokens obtained from the model#return response “`#### Checkpoint 3.2 Pick any ten examples from val500 such that five have correctly retrieved contexts, while the other five don’t. Print the LM-generated responses and the actual answers for them.—## SubmissionSubmit the following 6 files containing the output of your code as well as your code itself. Please use Brightspace:1. `a3_p1_lastname_id.py` 2. `a3_p1_lastname_id_OUTPUT.pdf` 3. `a3_p2_lastname_id.py` 4. `a3_p2_lastname_id_OUTPUT.pdf` 5. `a3_p3_lastname_id.py` 6. `a3_p3_lastname_id_OUTPUT.txt`**Please do not upload a zip file. Double-check that your files are there and correct after uploading and make sure to submit.** Uploading files that are zips or any other type than .py, .pdf, or .txt files will result in the submission being considered invalid. Partially uploaded files or non-submitted files will count as unsubmitted.## Tips An aggregation of answers to questions or tips given to students will appear here:– If using an approach where you control the padding, use left padding for text-generation (zero-shot) and right padding for inference/fine-tuning. – Suggested hyperparameter range: LR = 1e-6 to 1e-4, L2 penalty = 1e-4 to 1e-2. Selecting values outside this range is OK but may not be helpful. – When fine-tuning, often less than 5 epochs are necessary to update to work well with the new data/task. – Batch size is important. If your batch size is too big then the matrices won’t fit in memory and it will slow things down due to pulling them in and out of memory. Start with a batch size of 1 and then see if increasing slows things down or not. Also consider smaller batch-size means more regularization. – There are multiple ways to speed up training/fine-tuning: + Look into `torch.amp.autocast` to use FP16 precision during fine-tuning. It reduces the memory footprint which allows using a larger batch size. + Trimming sequences to the longest non-padded length in a mini-batch can be slightly faster than padding all sequences to the maximum length across the dataset. – Use GPU memory resourcefully by moving models and data to it only when necessary. Free up cache from GPU memory using `torch.cuda.empty_cache()`. – You are welcome to reference the run_clm (their abbreviation for generative language models, like gpt2) and run_mlm (encoding LMs, like roberta): https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling – HuggingFace uses “CLM” (causal language model) to refer to autoregressive language models (i.e., distilgpt2) and “MLM” (masked language model) to refer to autoencoding language models (i.e., distilroberta).

$25.00 View

[SOLVED] Assignment 2 cse538

Overview– **Part I. NGram LM (50 Points)** – 1.1 Tokenize (10 points) – 1.2 Smoothed Trigram Language Model (30 points) – 1.3 Perplexity of TrigramLM (10 points) – **Part II. RNN LM (50 Points)** – 2.1 Preparing the Dataset (10 points) – 2.2 Recurrent NN-based Language Model (10 points) – 2.3 Train RecurrentLM (20 points) – 2.4 Autoregressive Lyric Generation (10 points) – Extra Credit (10 points)#### Objectives: – Work with a pre-trained BPE tokenizer – Implement two different language modeling approaches – Evaluate the language models – Use a language model to generate content—## DatasetIn all parts of this assignment, you will use the same dataset covering task domains of language modeling and content generation.### Taylor Swift Lyrics Dataset [songs.csv](https://github.com/shaynak/taylor-swift-lyrics/blob/main/songs.csv) – **Description:** A dataset comprising all of Taylor Swift’s songs. The CSV file contains three columns – Title, Album, and Lyrics. The lyrics are in plain ASCII text, with the exception of a few accented characters in words like café, rosé, etc.## Requirements– You must use **Python version 3.8** or later with **PyTorch 2.1.1** or later and **Numpy 1.22** or later. – We will test your code. – You may do your development in any environment, but runtimes listed will be based on a **2 vCPU system with 1GB memory** and average hard disk drive (an [e2-micro](https://gcloud-compute.com/e2-micro.html) machine available in the free tier for students on GCP).### Colab Policy You are welcome to test your code in Colab to utilize the GPU or TPU available there by [uploading your code to drive and then importing it into Colab](https://colab.research.google.com/drive/1YppUP29n7S7w5rZahQeOCE2WVvTCdqys). However, **it is suggested you develop your code with a full code editor (e.g., VS Code, vim, emacs, PyCharm) rather than in Colab or any notebook.** Notebooks encourage a segmented style of coding, which often leads to bugs in AI systems. It is better to use modular and good object-oriented design within Python code files directly. Notebooks are useful for trying out brief segments of code.—## Python LibrariesAcceptable machine learning, or statistics libraries are listed below (see version numbers for torch and numpy above). Other data science, machine learning, or statistics related libraries are prohibited —- **ask if unsure**. The intention is for you to implement the algorithms we have gone over and problem-solve in order to best understand the concepts of this course and their practical application. **You may not use any pre-existing implementations of such algorithms even if they are provided in the following libraries.**“`python import random, os, sys, math, csv, re, collections, string import numpy as np import csvimport torch from torch import nn, Tensor import torch.nn.functional as F from sklearn.model_selection import train_test_split from sklearn.metrics import f1_scoreimport heapq import matplotlib “`(Note: Do not use `pandas`. It is not included above on purpose.)—## Code-Assistance PolicySee syllabus for code-assistance policy.**Copying code from another student (current or previous), person, or any non-open source is prohibited.** This will result in at least a zero on the assignment and a report to the graduate program director with possibility for more consequences.**Students are equally responsible for making sure their code is not accessible to any other student** as they are for not copying another’s code. Please see the syllabus for additional policies. A word to the wise: Multiple subparts of this assignment have never been given before. Take the time to figure them out on your own, seeking only conceptual help (or talking to TAs/instructor at office hours). This is not only less selfish to your classmates, but it will take you further in life.—## Part I: NGram LM (50 Points)Your objective is to implement a basic n-gram language model.– **Filename:** `a2_p1__.py` – **Input:** Your code should run without any _additional_ arguments. – Example: `python a2_p1_lastname_id.py songs.csv` – **Output:** Place checkpoint output into: `a2_p1___OUTPUT.txt`### 1.1 TokenizeAll components of this assignment will use a pre-trained BPE tokenizer with document start and stop tokens. In particular, parts 1 and 2 will use the GPT2 Tokenizer.– Start by initializing **[GPT2TokenizerFast](https://huggingface.co/docs/transformers/en/model_doc/gpt2#transformers.GPT2TokenizerFast)**, which is a BPE tokenizer. The “fast” version is quick in tokenizing batches of text sequences. It initially contains `` as the Beginning of Sentence (BOS), End of Sentence (EOS), and unknown token. – Add a pad token with value ``. – Change the BOS and EOS in the tokenizer’s special token map to `` and `` respectively.**Side Note:** GPT2 does not use start and stop tokens, and you must add them yourself. We are using GPT2 tokenizer here so you can compare probabilistic LM to the output of RNN-based LM in part 2. However, if you were to want to use a tokenizer that has sentence start and stop tokens, like roberta-base, then you would get them with: “`python tokenizer = PreTrainedTokenizerFast.from_pretrained(‘roberta-base’)words = tokenizer.convert_ids_to_tokens(tokenizer.encode(“When did SBU open?”)) “`Load the dataset from [songs.csv](https://github.com/shaynak/taylor-swift-lyrics/blob/main/songs.csv), keeping all except the last 5 songs as the training set. We will use pieces of the last 5 songs for out-of-sample testing.Note: You may use the following code to read the dataset (it excludes the header and last 5 songs) for both parts.“`python with open(‘songs.csv’, newline=”) as f: reader = csv.reader(f) data = list(reader)[1:-5] “`#### Checkpoint 1.1 Mark the checkpoint clearly in your output: `print(“ Checkpoint 1.1:”)`Print the token list for the “Lyrics” column of the first and last rows of the training set. “` first: [‘This’, ‘Ġis’, ‘Ġa’, ‘Ġnewer’, ‘Ġtest’, ‘Ġof’, ‘Ġtoken’, ‘izing’, ‘Ġfrom’, ‘ĠSt’, ‘ony’, ‘ĠBrook’, ‘ĠUniversity’, ‘.’] last: [‘S’, ‘ton’, ‘y’, ‘ĠBrook’, ‘ĠUniversity’, ‘Ġwas’, ‘Ġfounded’, ‘Ġand’, ‘Ġfirst’, ‘Ġheld’, ‘Ġclasses’, ‘Ġin’, ‘Ġ1967’, ‘.’, ‘Ġ’, ‘ĠWhen’, ‘Ġdid’, ‘ĠSB’, ‘U’, ‘Ġopen’, ‘?’] “` Note: Above is an example output, not the answer. Remember that Ġ is the space marker### 1.2 Smoothed Trigram Language ModelMake a class, `TrigramLM`, that creates and trains a trigram language model. The LM should be trained on all songs from the training set. In particular, `TrigramLM` must have the following instance methods:#### **Methods:** 1. `trigramLM.train(datasets)`: Stores data necessary to be able to compute smoothed trigram and unigram probabilities (see more specifics below). Does not need to return anything. – Dataset is a collection of songs. These can be in any format that suits your approach. 2. `trigramLM.nextProb(history_toks, next_toks)`: Returns the probability of tokens in `next_toks`, given `history_toks`. – `history_toks` is a list of previous tokens. Make sure this works for lists of any size, including 0 (i.e. no history: unigram model) to a size > 2 in which case it just uses the final 2 history tokens since it is a trigram model (this will be helpful for future work). – `next_toks` is a set of possible next tokens to return probabilities for.**Unlike assignment 1 you may add additional optional arguments but you must have the above arguments.** The LM must have the following properties: 1. The vocabulary must be the complete vocabulary of the tokenizer. 2. It must utilize an “OOV” token when a word is not available or an ngram count is not available. 3. You must use document start and stop tokens. 4. During training it must store **what is necessary for computing** add-one smoothed trigram and unigram probabilities.#### Checkpoint 1.2 Print the `nextProb` for the following histories and candidate next tokens:1. `history_toks=[‘’, ‘Are’, ‘Ġwe’]` `next_toks=[‘Ġout’, ‘Ġin’, ‘Ġto’, ‘Ġpretending’, ‘Ġonly’]` 2. `history_toks=[‘And’, ‘ĠI’]` `next_toks=[‘Ġwas’, “‘m”, ‘Ġstood’, ‘Ġknow’, ‘Ġscream’, ‘Ġpromise’]`### 1.3 Perplexity of TrigramLM (10 points)Perplexity is a metric to measure how “surprised” a language model is when it sees a test sample. Place your code in `get_perplexity(probs)` to compute this metric as the inverse of the geometric mean over the probabilities of target tokens.“`python def get_perplexity(probs): # input: probs: a list containing probabilities of the target token for each index of input # output: perplexity: a single float number# return perplexity “`#### Checkpoint 1.3 Print the perplexity for the following cases:1. `[‘And’, ‘Ġyou’, ‘Ġgotta’, ‘Ġlive’, ‘Ġwith’, ‘Ġthe’, ‘Ġbad’, ‘Ġblood’, ‘Ġnow’]` 2. `[‘Sit’, ‘Ġquiet’, ‘Ġby’, ‘Ġmy’, ‘Ġside’, ‘Ġin’, ‘Ġthe’, ‘Ġshade’]` 3. `[‘And’, ‘ĠI’, “‘m”, ‘Ġnot’, ‘Ġeven’, ‘Ġsorry’, ‘,’, ‘Ġnights’, ‘Ġare’, ‘Ġso’, ‘Ġstar’, ‘ry’]` 4. `[‘You’, ‘Ġmake’, ‘Ġme’, ‘Ġcraz’, ‘ier’, ‘,’, ‘Ġcraz’, ‘ier’, ‘,’, ‘Ġcraz’, ‘ier’, ‘,’, ‘Ġoh’]` 5. `[‘When’, ‘Ġtime’, ‘Ġstood’, ‘Ġstill’, ‘Ġand’, ‘ĠI’, ‘Ġhad’, ‘Ġyou’]`What are your observations about these results? Are the values similar or different? What is one major reason for this? (2-4 lines).*Code should run in under 3 minutes, although it is possible to run in less than 1 minute.*—## Part II. RNN LM (50 Points)In this part, your objective is to implement the language modeling task using a recurrent neural network.– **Filename:** `a2_p2__.py` – **Input:** Your code should run without any additional arguments + Example: `a2_p2_lastname_id.py songs.csv` – **Output:** Place checkpoint output into: `a2_p2___OUTPUT.pdf`### 2.1 Preparing the datasetNow fill in the `chunk_tokens` function below. The function should split a larger token sequence into multiple, smaller sequences of size `chunk_len – 2`. Append the BOS token id at the start and EOS token id at the end of the chunk. If the number of tokens are less than chunk_size, then append pad tokens after EOS.Same sequence length enables batch processing, which saves time during model training.“`python def chunk_tokens(tokens, start_token_id, end_token_id, pad_token_id, chunk_len=128): # input: tokens: a list containing token ids # start_token_id, end_token_id, pad_token_id: special token ids from the tokenizer # chunk_len: the length of output sequences # output: chunks: torch.tensor of sequences of shape (#chunks_in_song, chunk_len) # return chunks “`Load [songs.csv](https://github.com/shaynak/taylor-swift-lyrics/blob/main/songs.csv) excluding the last 5 songs. Process each row in the training set’s “Lyrics” column to obtain a tensor of shape (#chunks_in_song, chunk_len): 1. Remove section markers such as [Bridge], [Chorus], etc. by using the regex pattern `r’ [[x20-x7f]+]’` to replace it with an empty string. 2. Tokenize current row’s lyrics to get a list of token ids. 3. Call `chunk_tokens` on the list of token ids using `chunk_len = 64`.Stack each row’s resultant tensor into a single tensor of shape (#all_chunks, chunk_len). Use this to create **X** and **y** for self-supervised learning. **X** should contain all but the last column, whereas **y** excludes the first column. In other words, a column in y gives the next token for corresponding current token in X.Use `torch.utils.data.TensorDataset` and `torch.utils.data.DataLoader` with batch size of 32 and `drop_last=True`. It makes batching the dataset more convenient to manage.#### Checkpoint 2.1 Print the chunked tensors for the song “Enchanted (Taylor’s Version)” from the album “Speak Now (Taylor’s Version)”.### 2.2 Recurrent NN-based Language ModelPlace in the code for `RecurrentLM`, which is a GRU-based language model. 1. `__init__` – takes as input the dimensions for various layers and initializes them. 2. `forward` – accepts a batched sequence of token IDs. Process the input **x** in the order of layers Embedding -> GRU-> Layer norm -> Fully-connected. 3. `stepwise_forward` – complete this in Part 2.4“`python class RecurrentLM(nn.Module): def __init__(self, vocab_size, embed_dim, rnn_hidden_dim): super().__init__() # – An embedding layer to convert token IDs into vectors of size embed_dim # – An GRU layer with rnn_hidden_dim sized state vectors. Use batch_first=True # – Layer normalization for RNN’s outputs # – Projection from rnn_hidden_size to vocab_sizedef forward(self, x): # input: x: tensor of shape (batch_size, seq_len) # output: logits: output of the model. # hidden_state: hidden state of GRU after processing x (sequence of tokens) # return logits, hidden_state# def stepwise_forward(self, x, prev_hidden_state): #input: x: tensor of shape (seq_len) # hidden_state: hidden state of GRU after processing x (single token) ## return logits, hidden_state “`#### Checkpoint 2.2 Print the shape of **logits** and **hidden_state** tensors in terms of `batch_size`, `chunk_len`, `vocab_size` and `rnn_hidden_dim`.### 2.3 Train RecurrentLMInitialize `RecurrentLM` with: 1. vocab_size as size of the tokenizer’s vocabulary (including special tokens) 2. `embed_dim = 64` 3. `rnn_hidden_dim = 1024`Train the model on GPU using mini batch gradient descent, passing features from 2.1 with batch size of **32**. Use a learning rate **0.0007** with the Adam optimizer. Train the model for **15** epochs.Next-token prediction is also a classification problem, which allows us to use Cross-entropy loss. Transform the logits and labels such that their dimensions are (#examples, #classes). Now we can directly use `nn.CrossEntropyLoss`, but before that, there is one more detail to take care of!In Part 2.1, pad tokens were appended to sequences shorter than `chunk_len`. These should be handled so that they don’t contribute to a model’s predictions and loss calculations. Filter out the outputs and labels corresponding to indices where the input was a pad token ID. Now, compute the loss on the remaining set of inputs, outputs, and labels.Place your code in a method named `trainLM`.“`python def trainLM(model, data, pad_token_id, learning_rate, device): # input: model – instance of RecurrentLM to be trained # data – contains X and y as defined in 2.1 # pad_token_id – tokenizer’s pad token ID for filtering out pad tokens # learning_rate # device – whether to train model on CPU (=”cpu”) or GPU (=”cuda”) # output: losses – a list of loss values on the train data from each epoch #return losses “`#### Checkpoint 2.3 1. Plot the training set loss curves. It should have loss on the y-axis and epochs on the x-axis. Paste the loss curve into your output file and save it as a pdf. 2. Compute the perplexity of the model on the samples (you can use `get_perplexity` from Part 1.3) – `”And you gotta live with the bad blood now”` – `”Sit quiet by my side in the shade”` – `”And I’m not even sorry, nights are so starry”` – `”You make me crazier, crazier, crazier, oh”` – `”When time stood still and I had you”` 3. Compare the perplexity scores with Part 1.3. How does the RNN-based LM perform in comparison? Provide a brief reason why it is or isn’t better.### 2.4 Autoregressive Lyric GenerationRevisit `RecurrentLM` class definition and place your code for `stepwise_forward`. This function accepts a single token ID as input and the hidden state from the previous step to predict the next token. This method is mostly similar to forward, except that now you have to pass the hidden state to the GRU.Next, place your code in the `generate` method which should 1. Tokenize the `start_phrase`. Since this could be one or more tokens, process it with RecurrentLM.forward to get the logits and hidden state. Pick the logit with the highest probability as the next token and append its ID to `generated_tokens`. 2. Pass this token along with the hidden state to `RecurrentLM.stepwise_foward`. Like the previous step, capture the highest probability logit as the next token. Repeat this process until either `max_len` number of tokens are generated (or) the generated token is EOS/pad.Use a `max_len = 64`.“`python def generate(model, tokenizer, start_phrase, max_len, device): # input: model – trained instance of RecurrentLM # tokenizer # start_phrase – string containing input word(s) # max_len – max number of tokens to generate # device – whether to inference model on CPU (=”cpu”) or GPU (=”cuda”) # output: generated_tokens – list of generated token IDs #return generated_tokens “`#### Checkpoint 2.4 Print the content generated with the following start phrases in plain text (not token IDs!) 1. `”Are we”` 2. `”Like we’re made of starlight, starlight”` 3. Try your own start phrase here#### Extra Credit 1. Modify `generate` to sample the next token from the probability distribution over all logits instead of taking the highest logit. Generate content with start phrases from Checkpoint 2.4 and print it. 2. Which approach appears to generate more original/unique content and why?*Code should complete steps through 2.4 within 5-7 minutes (based on Colab GPU; note see colab policy).*—## SubmissionSubmit the following 4 files containing the output of your code as well as your code itself. Please use brightspace: 1. `a2_p1_lastname_id.py` 2. `a2_p1_lastname_id_OUTPUT.txt` 3. `a2_p2_lastname_id.py` 4. `a2_p2_lastname_id_OUTPUT.pdf`**Please do not upload a zip or a notebook file. Double-check that your files are there and correct after uploading and make sure to submit.** Uploading files that are zips or any other type than .py, .pdf, or .txt files will result in the submission being considered invalid. Partially uploaded files or non-submitted files will count as unsubmitted.If submitting multiple times, only the last submission will be graded. If it is late, the penalty will apply even if earlier submissions were made.**Questions:** Please post questions to the course forum.—## Additional References

$25.00 View

[SOLVED] Assignment 1 cse538

Overview– **Part I. Tokenizing (40 Points)** – **Part II. Part-of-Speech Tagging (60 Points)**Objectives: – Practice using regular expressions – Implement a tokenizer – Come to understand the pros and cons of different tokenizers – there is no perfect tokenizer but there are better and worse tokenizers – Practice implementing a PyTorch model – Become more familiar with logistic regression/maximum entropy classification – Experiment with regularization techniques to better understand how they work—## DatasetThe Twitter POS annotated data was introduced in the paper [Part-of-Speech Tagging for Twitter: Annotation, Features, and Experiments](https://aclanthology.org/P11-2008.pdf). Within, you will find many files and you may look at the README.txt for a brief explanation. For the assignment, we will be working with **Daily547**, which is a compilation of 547 unique tweets.### Part 1: [daily547_tweets.txt](https://drive.google.com/file/d/1OwbJQE2CpsCKuBGLu67PEobHQbCCBWUU/view) – **Description:** The file contains the 547 tweets, each on a different line. Consider each tweet a “document” that you will tokenize and store.### Part 2: [daily547_3pos.txt](https://drive.google.com/file/d/1u6iqtwkaEbJuMjk14QTw2X5sYDdZG9yL/view) – **Description:** Each line in this file contains either (1) a tab-separated token and its POS label, or (2) an empty line between the last and first tokens to denote the end of a sentence. – While the original version [daily547.conll](https://github.com/brendano/ark-tweet-nlp/blob/master/data/twpos-data-v0.3/daily547.conll) has 25 POS tags (defined in the linked paper), for this assignment we have aggregated them into **3 classes** as follows: 1. **Noun-related (N)** – originally labeled: N, O, S, ^, Z, L, M, A 2. **Verb (V)** – originally labeled: V 3. **Other (O)** – originally labeled: R ! D P & T X Y # @ ~ U E $ ‘,’ G## Requirements– You must use **Python version 3.8** or later with **PyTorch 2.1.1** or later and **Numpy 1.22** or later. – We will test your code. – You may do your development in any environment, but runtimes listed will be based on a **2 vCPU system with 1GB memory** and average hard disk drive (an [e2-micro](https://gcloud-compute.com/e2-micro.html) machine available in the free tier for students on GCP).—## Python LibrariesAcceptable machine learning, or statistics libraries are listed below (see version numbers for torch and numpy above). Other data science, machine learning, or statistics related libraries are prohibited —- **ask if unsure**. The intention is for you to implement the algorithms we have gone over and problem-solve in order to best understand the concepts of this course and their practical application. **You may not use any pre-existing implementations of such algorithms even if they are provided in the following libraries.**“`python import random, os, sys, math, csv, re, collections, string import numpy as npimport torch from torch import nn, Tensor import torch.nn.functional as F from sklearn.model_selection import train_test_split from sklearn.metrics import f1_scoreimport heapq import matplotlib “`(Note: Do not use `pandas`. It is not included above on purpose.)—## Code-Assistance PolicySee syllabus for code-assistance policy.**Copying code from another student (current or previous), person, or any non-open source is prohibited.** This will result in at least a zero on the assignment and a report to the graduate program director with possibility for more consequences.**Students are equally responsible for making sure their code is not accessible to any other student** as they are for not copying another’s code. Please see the syllabus for additional policies. A word to the wise: Multiple subparts of this assignment have never been given before. Take the time to figure them out on your own, seeking only conceptual help (or talking to TAs/instructor at office hours). This is not only less selfish to your classmates, but it will take you further in life.—## Part I. Tokenizing (40 Points)Your objective is to develop and compare **tokenization approaches** for messy social media data.– **Filename:** `a1_p1__.py` – **Input:** The file to be tokenized. + Example: `a1_p1_lastname_id.py daily547_tweets.txt` – **Output:** Place checkpoint output into: `a1_p1___OUTPUT.txt` – **Mark the checkpoint clearly in your output: `print(“Checkpoint X.X:”)`**### 1.1 Word RegEx TokenizerWrite your own word tokenizer using regular expressions that fulfills the following rules:1. Retain capitalization. 2. Separate punctuation from words, except for: – Abbreviations of capital letters (e.g. `“U.S.A.”`) – Contractions (e.g. `“can’t”`) – Periods surrounded by integers (e.g. `”5.0″`) Examples: `“with the fork.”` as `[“with”, “the”, “fork”, “.”]` `“Section 5.”` as `[“Section”, “5”, “.”]` 3. Allow for hashtags and @mentions as single words (e.g. `#sarcastic`, `@sbunlp`)You may handle any situation not covered by these rules however you like.Place your code in a method named `wordTokenizer`.“`python def wordTokenizer(sent): # input: a single sentence as a string. # output: a list of each “word” in the text # must use regular expressions# return tokens “`You may create other methods that this method calls.#### Checkpoint 1.1 Print the output of `wordTokenizer` on the first 5 documents and the very last document.### 1.2 Spaceless BytePair TokenizerCreate a BytePair pair encoding tokenizer that does not include whitespace as a valid character. That is, it will not allow a space in any “word.” and only use space as indicating a definite split between two words in the corpus.Example: if the vocabulary was `[“i”, “n”, “a”, “b”, “in”]` and the corpus was `”abin bi nab ain”` it would be tokenized as: `[“a”, “b”, “in”, “b”, “i”, “n”, “a”, “b”, “a”, “in”]` ← note that “i” and “n” was not joined in `”bi nab”` because `”i”` was already separated from `”n”`.Otherwise, byte pair encoding will proceed as described in the book and slides: Start with a vocabulary of all ascii letters as words. Convert all non-ascii characters into “?”.Run until the vocabulary size reaches 1000.Place code that learns the vocabulary in a method named `spacelessBPElearn`, and place code that runs the tokenizer in a method named `spacelessBPETokenize`.“`python def spacelessBPELearn(docs, max_vocabulary=1000): # input: docs, a list of strings to be used as the corpus for learning the BPE vocabulary # output: final_vocabulary, a set of all members of the learned vocabularyreturn final_vocabularydef spacelessBPETokenize(text, vocab): # input: text, a single string to be word tokenized. # vocab, a set of valid vocabulary words # output: words, a list of strings of all word tokens, in order, from the stringreturn words “`You may create other methods that this method calls (for example, training the tokenizer should probably).#### Checkpoint 1.2 Print the top five most frequent pairs at iterations 0, 1, 10, 100, and 500. Print the final vocabulary. Print the tokenization of the first 5 documents and the very last document.*Code should run in under 2 minutes, although it is possible to run in only a few seconds.*—## Part II. Part-of-Speech Tagging (60 Points)In this part, your objective is to [determine the part of speech for each token in a sentence](https://en.wikipedia.org/wiki/Part-of-speech_tagging) by developing a simple neural network from scratch. Writing code for the training and evaluation of a barebones model using the given function outlines will serve as a stepping stone towards using PyTorch in the next assignment.– **Filename:** `a1_p2__.py` – **Input:** Your code should run without any additional arguments. + Example: `a1_p2_lastname_id.py daily547_3pos.conll` – **Output:** Place checkpoint output into: `a1_p2___OUTPUT.txt`### 2.0 Loading the DatasetUse the function `getConllTags` to load the data from daily547_3pos.txt. The function will return a list of sentences containing (token, POS tag) pairs.“`python def getConllTags(filename): # input: filename for a conll style parts of speech tagged file # output: a list of list of tuples [sent]. representing [[[word1, tag], [word2, tag2]]wordTagsPerSent = [[]] sentNum = 0 with open(filename, encoding=’utf8′) as f: for wordtag in f: wordtag=wordtag.strip() if wordtag: # still reading current sentence (word, tag) = wordtag.split(“t”) wordTagsPerSent[sentNum].append((word,tag)) else: # new sentence wordTagsPerSent.append([]) sentNum+=1 return wordTagsPerSent “`Next, create a dictionary of all unique tokens by iterating over every (token, POS tag) pair, mapping each unique token to an index (e.g. `{‘hello’:0, ‘world’:1}`). Create a similar ID mapping for all POS tags.We will use these mappings to create one-hot encodings in the next sections.### 2.1 Lexical Feature SetGiven the target index into tokens in a sentence, return a vector for that token having the following features:1. Whether the first letter of the target is capitalized (binary feature) 2. The first letter of the target word (i.e. word at index `targetI`): use 257 one-hot values: 0 through 255 can be used simply as ascii values and 256 can be used for anything non-ascii (i.e. when `ord(char) > 255`). 3. The normalized length of the target word as an integer. Normalize the length as: `min(token_length, 10)/10` 4. One-hot representation of previous word (note: no feature will be 1 if target is first word) Use `wordToIndex` to make sure the one-hot representation is consistent. `wordToIndex` is a dictionary that, given a word, returns the integer to make 1 in the one-hot representation. If the word is not in `wordToIndex` then all will be zero. 5. One-hot representation of current word 6. One-hot representation of next word (note: no feature will be 1 if target is last word)All features should be concatenated into one long flat vector.“`python def getFeaturesForTarget(tokens, targetI, wordToIndex): # input: tokens: a list of tokens in a sentence, # targetI: index for the target token # wordToIndex: dict mapping ‘word’ to an index in the feature list. # output: list (or np.array) of k feature values for the given target#return featureVector “`Process each token in the dataset using `getFeaturesForTarget` to obtain a feature matrix (**X**) with the dimensions (num. of all tokens, feature vector length). Then, create ground-truth vector (**y**) with the indices of POS tag IDs using the mapping defined in section 2.0.Split the dataset (**X** and **y**) into 70% train and 30% dev subsets.#### Checkpoint 2.1 Print the sum of the first and last 5 individual feature vectors of **X**.### 2.2 Train Logistic RegressionImplement multiclass logistic regression training using features from 2.1. Use a learning rate and L2 penalty 0.01 with the SGD optimizer and train the model for 100 epochs. You need to use the full training set and not mini batches during forward pass.Place your code in a method named `trainLogReg`.“`python def trainLogReg(train_data, dev_data, learning_rate, l2_penalty): # input: train/dev_data – contain the features and labels for train/dev splits # input: learning_rate, l2_penalty – hyperparameters for model training # output: model – the trained pytorch model # output: train/dev_losses – a list of train/dev set loss values from each epoch # output: train/dev_accuracies – a list of train/dev set accuracy from each epochreturn model, train_losses, train_accuracies, dev_losses, dev_accuracies “`#### Checkpoint 2.2 Plot the training and dev set loss and accuracy curves. The plots have epochs as the x-axis and loss/accuracy as the y-axis. Paste the loss curve into your output text file and save it as a pdf.### 2.3 Hyperparameter TuningImprove your model by tuning hyperparameters related to learning rate and regularization. Using grid search for the model over dev set, you will try to find the best learning rates, trying `[0.1, 1, 10]` as well as find the best L2 penalty, trying `[1e-5, 1e-3, 1e-1]`. Find the dev set accuracy over for each configuration.Place your code in a method named `gridSearch`, which is able to evaluate any model it takes as input:“`python def gridSearch(train_set, dev_set, learning_rates, l2_penalties): # input: learning_rates, l2_penalties – each is a list with hyperparameters to try # train_set – the training set of features and outcomes # dev_set – the dev set of features and outcomes # output: model_accuracies – dev set accuracy of the trained model on each hyperparam combination # best_lr, best_l2_penalty – learning rate and L2 penalty combination with highest dev set accuracyreturn model_accuracies, best_lr, best_l2_penalty “`Next, train the best model using the best hyperparameters from `gridSearch` to obtain its loss and accuracy curves.#### Checkpoint 2.3 1. Print a table with the dev set accuracy values for all combinations of: – columns: l2 penalty – rows: learning rate as follows| LR L2 | 1e-5 | 1e-3 | 1e-1 | |———-|——-|——-|——-| | 0.1 | x | x | x | | 1 | x | x | x | | 10 | x | x | x |Print the combination that worked the best.2. Include the loss and accuracy curve from the model trained with best hyperparameters.Your best dev set accuracy should be above 0.75 (i.e. 75%) or else something is off (it’s possible for the best to be even a couple points greater than this).### 2.4 Best Model InferenceTo verify how well the model is working, we will test it on some unseen examples.Tokenize the sentences given below using `wordTokenizer` (section 1.1) and generate the feature vectors for each token using `getFeaturesForTarget` (section 2.1). Use the best model to predict the POS tags.“`python sampleSentences = [‘The horse raced past the barn fell.’, ‘For 3 years, we attended S.B.U. in the CS program.’, ‘Did you hear Sam tell me to “chill out” yesterday? #rude’] “`#### Checkpoint 2.4 1. For each sample sentence, print the POS tag predicted for each token obtained from `wordTokenizer`. 2. What is your observation about the qualitative performance of the best model?*Code should complete steps through 2.4 within 7-8 minutes (based on Colab CPU).*—## Additional References1. [Summary of the tokenizers by Hugging Face](https://huggingface.co/transformers/v4.5.1/tokenizer_summary.html) 2. [Byte Pair Encoding and Data Structures](https://guillaume-be.github.io/2021-09-16/byte_pair_encoding) 3. [Introducing a faster, more flexible byte-pair tokenizer by GitHub](https://github.blog/ai-and-ml/llms/so-many-tokens-so-little-time-introducing-a-faster-more-flexible-byte-pair-tokenizer/)

$25.00 View